c++计算总是打印出11
C++ Calculation always printing out 11
我目前正在编写一个程序,根据用户输入的ISBN的一部分的数字为用户提供校验和。我对13位ISBN的计算得到了正确的答案,但出于某种原因,对10位ISBN的计算总是得到11,无论我输入什么。
cout << "Enter the digits one at a time, as if they were to be read from right to left." << endl;
cin >> digit_one;
cin >> digit_two;
cin >> digit_three;
cin >> digit_four;
cin >> digit_five;
cin >> digit_six;
cin >> digit_seven;
cin >> digit_eight;
cin >> digit_nine;
checksum = (11 - (((10, 9, 8, 7, 6, 5, 4, 3, 2) * (digit_one, digit_two, digit_three, digit_four, digit_five, digit_six, digit_seven, digit_eight, digit_nine)) % 11));
cout << "The checksum for this ISBN is " << checksum << endl;
我是否错过了一些简单的东西?提前感谢您的帮助
您似乎误解了操作符,
的工作原理。(2, 3)
生成3
,而不是Python中的元组。
同样,(2,3,4) * (1,2,3)
与4 * 3
几乎相等。
在你的代码中
checksum = (11 - (((10, 9, 8, 7, 6, 5, 4, 3, 2) * (digit_one, digit_two, digit_three, digit_four, digit_five, digit_six, digit_seven, digit_eight, digit_nine)) % 11))
和
没有区别checksum = (11 - ((2 * digit_nine) % 11))
那么可以安全地假设您的digit_nine
是0
¹,这就是为什么最终checksum
的值为11 - 0
¹或11
的乘法,而不是数字
相关文章:
- 如何循环打印顶点结构
- C++计算未以正确的格式打印
- 打印给定大小的集合的所有子集并计算子集
- 从右到左计算当前索引中的数据结构中的元素数,并打印每个元素的计数
- 我正在计算的序列(Hailstone)需要打印一次时两次打印
- Arduino:在库中计算出的打印值
- C++二叉搜索树打印和深度计算
- 计算各种数字中的最小值倍数,并将其打印为prime
- 计算文本文件中的单个字母并打印结果 - 错误的输出 C++
- C++一次打印一个单词的字符串,计算字符数和字符数
- 计算出现次数并使用C/STL打印顶部K
- 这个 c++ 程序中缺少什么?打印计算
- 如何在C++中打印编译时计算的结果
- 计算并打印句子中每个单词的长度
- GDB :使用打印计算双倍
- c++计算输入和保存点的结构与原点之间的距离,根据距离打印出列表
- c++计算总是打印出11
- 打印序列中的1的个数,直到一个数字,而不实际计算1
- 如何打印或计算张量
- 如何粗略计算和打印clock_t时间