我目前正在编写一个程序,根据用户输入的ISBN部分的数字,给用户一个校验和。我对13位ISBN的计算给了我正确的答案,但出于某种原因,10位数的计算总是给我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;我错过了什么简单的东西吗?谢谢你提前帮忙。
发布于 2016-10-09 20:32:01
您似乎误解了操作员,的工作方式。(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是01,这就是为什么最终checksum具有11 - 0的值
或者11的乘法,一点也不算数字。
https://stackoverflow.com/questions/39948224
复制相似问题