gpt4 book ai didi

C++计算总是打印出11

转载 作者:太空宇宙 更新时间:2023-11-04 14:39:05 25 4
gpt4 key购买 nike

我目前正在编写一个程序,根据用户输入的部分 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_nine0¹,这就是为什么最终 checksum 的值为 11 - 0

¹ 或 11 的乘法,根本不是数字

关于C++计算总是打印出11,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39948224/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com