我的名字乔和我遇到了一些关于c ++模数的问题
继承人的问题:
#include <iostream>
#include <string>
using namespace std;
int main()
{
//In short, this is what i am trying to do:
//divide two numbers, and get both the quotient
//and the remainder
//however, as an example, this below produces a remainder
//of 10
//110 \ 20 should equal 5 remainder 5
int firstInput =110, secondInput = 20;
int quotient = 0, remainder = 0;
quotient = firstInput / secondInput;
remainder = firstInput % secondInput;// i think the problem is here
cout << "The quotient is " << quotient << " and the remainder is "
<< remainder << endl;
system("pause");
return 0;
}
基本上它没有正确计算余数。 任何帮助当然会非常感激。欢呼声
答案 0 :(得分:7)
我得到了正确答案......
The quotient is 5 and the remainder is 10
Press any key to continue . . .
我认为这个错误可能位于键盘和椅子之间...... = P
答案 1 :(得分:1)
110 = 5 * 20 + 10
余下的是 10,而不是5
答案 2 :(得分:1)
110 = 5 * 20 + 10,因此商为5,余数为10。 所以它似乎正确计算。
如果你想得到“休息”0.5,你需要计算((double)firstInput/secondInput) - (firstInput/secondInput)
。
答案 3 :(得分:0)
如果你将110除以20,你 应该得到5的商和10的剩余...因为:
5*20 + 10 = 110
答案 4 :(得分:-1)
尝试fmod以获得你想要的东西。