C/C++负数除法和负数取模

这几道题目的运行结果是多少

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 int main() {
 4     cout << "1: " << 7 / 4 << endl;
 5     cout << "2: " << (-7) / 4 << endl;
 6     cout << "3: " << 7 / (-4) << endl;
 7     cout << "4: " << (-7) / (-4) << endl;
 8     cout << "5: " << 7 % 4 << endl;
 9     cout << "6: " << (-7) % 4 << endl;
10     cout << "7: " << 7 % (-4) << endl;
11     cout << "8: " << (-7) % (-4) << endl;
12     return 0;
13 } 

正确答案是

总结下来就是:

1.除法向零取整

2.取模运算按照余数=被除数-商×除数计算。也可记为,取模运算结果的符号,取决于被模数的符号,与模数的符号无关。

转载自https://blog.csdn.net/eastlhu/article/details/51536331

原文地址:https://www.cnblogs.com/fx1998/p/13304128.html