西安电子大学计算机考研复试机试(2019)+ 算法笔记 + 数学问题(gcd/最小公倍数/分数等)
2019-04-13 21:03发布
生成海报
/*最大公约数*/
int gcd(int a,int b){
if(b == 0) return a;
return gcd(b,a%b);
}
/*最小公倍数*/
cout<
递归+出口
#include
#include
#include
using namespace std;
/*最大公约数*/
int gcd(int a,int b){
if(b == 0) return a;
return gcd(b,a%b);
}
struct Fraction{
int up,down;
};
/*分数的化简*/
Fraction fun(Fraction result){
if(result.down<0){
result.down = -result.down;
result.up = -result.up;
}
if(result.up == 0){
result.down = 1;
}else{
int d = gcd(abs(result.up),abs(result.down));
result.up /=d;
result.down /=d;
}
return result;
}
/*分数的加法*/
Fraction add(Fraction x1,Fraction x2){
Fraction result;
x1 = fun(x1);
x2 = fun(x2);
result.up = x1.up*x2.down+x1.down*x2.up;
result.down = x1.down*x2.down;
return fun(result);
}
int main(){
Fraction result,x1,x2;
while(cin>>x1.up>>x1.down>>x2.up>>x2.down){
result = add(x1,x2);
/*分数的输出*/
if(result.down == 1){
cout<
打开微信“扫一扫”,打开网页后点击屏幕右上角分享按钮