现在的位置: 首页 > 综合 > 正文

编写函数实现求余运算

2014年11月19日 ⁄ 综合 ⁄ 共 260字 ⁄ 字号 评论关闭
#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

抱歉!评论已关闭.