#include<iostream> using namespace std; int mod(int x, int y) { return x - x / y * y; } int main() { cout << 8 % 3 << endl; cout << mod(8, 3) << endl; cout << 8 % -3 << endl; cout << mod(8, -3) << endl; cout << -8 % -3 << endl; cout << mod(-8, -3) << endl; cout << -8 % 3 << endl; cout << mod(-8, 3) << endl; return 0; }
结果为:
2
2
2
2
-2
-2
-2
-2