题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2503
题目大意:有a,b,c,d四个整数,求a/b+c/d的最简分数
关键思想:通分化简GCD
代码如下:
//GCD通分化简 #include <iostream> using namespace std; int gcd(int a,int b){ return a%b==0?b:gcd(b,a%b); } int main(){ int T; int a,b,c,d; int up,down; //分子分母 cin>>T; while(T--){ cin>>a>>b>>c>>d; down=b*d; up=a*d+c*b; int t=gcd(up,down); up/=t,down/=t; //约分 cout<<up<<" "<<down<<endl; } return 0; }