编写函数实现求余运算

来源:互联网 发布:java|| 编辑:程序博客网 时间:2024/06/06 01:22
#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