标签:求商求模运算
这两种运算总是感觉有点迷糊,现在 拨开乌云见天日。
对于整型数a,b来说,取模运算或者求余运算的方法都是:
1.求 整数商: c = a/b;
2.计算模或者余数: r = a - c*b.
#include <iostream> using namespace std; void div(){ printf("5/3: %d",5/3);cout << endl; printf("5/(-3): %d",5/-3);cout << endl; printf("(-3)/8: %d",-3/5);cout << endl; printf("3/(-8): %d",3/-5);cout << endl; } void mod(){ printf("5取模3: %d",5%3);cout << endl; printf("5取模-3: %d",5%-3);cout << endl; printf("-3取模5: %d",-3%5);cout << endl; printf("3取模-5: %d",3%-5);cout << endl; } void main(){ cout<<"求商运算"<<endl; div(); cout<<endl; cout<<"求模运算"<<endl; mod(); }
标签:求商求模运算
原文地址:http://blog.csdn.net/buyingfei8888/article/details/38359151