分值 : 20 难度 : 中等题 思路 : 辗转相除求最大公约数 坑点 : 很多判断条件,题目很烦,因此会有坑,例如你读进来的数也许就需要你进行约分才能输出
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
#include <iostream>using namespace std;long long a,b,A,B ;bool INF = false ;void print_result(long v1 , long v2){ if(v2 ==0 ) { cout <<"Inf"; INF =true ; return ; } bool flag = false ; if(v1<0&&v2>0) { flag = true ; v1 = -v1 ; } if(v1>0&&v2<0) { flag = true ; v2 = -v2 ; } long long cur_b = v1 > v2 ? v2 : v1 ; long long cur_a = v1 > v2 ? v1 : v2 ; long long r =1 ,C ; while(cur_b!=0) { C = cur_a / cur_b ; r = cur_a % cur_b ; cur_a = cur_b ; cur_b = r ; } v1/= cur_a ; v2/= cur_a ; if(flag) cout <<"(-"; if(v1/v2) cout<< v1/v2 ; if(v1/v2 && v1%v2) cout <<" "; if(v1%v2) cout<<v1%v2<<"/"<<v2; if(!(v1/v2) && !(v1%v2)) cout << "0" ; if(flag) cout<<")" ; return ;}int main() { scanf("%ld/%ld %ld/%ld" , &a ,& b ,&A ,&B ); // + print_result(a,b); cout<<" + "; print_result(A,B); cout<<" = "; if(!INF) print_result(B*a+A*b , B*b); else cout <<"Inf" ; cout <<endl ; print_result(a,b); cout<<" - "; print_result(A,B); cout<<" = "; if(!INF) print_result(B*a-A*b , B*b); else cout <<"Inf" ; cout <<endl ; print_result(a,b); cout<<" * "; print_result(A,B); cout<<" = "; if(!INF) print_result(A*a , B*b); else cout <<"Inf" ; cout <<endl ; print_result(a,b); cout<<" / "; print_result(A,B); cout<<" = "; if(!INF) print_result(B*a , A*b); else cout <<"Inf" ; cout <<endl ;}