来源:HDU2503
水题:
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <map>
using namespace std;
typedef long long LL;
int gcd(int a,int b){
if(b==0) return a;
else
return gcd(b,a%b);
}
int main(){
int T;
scanf("%d",&T);
while(T--){
int a,b,c,d;
scanf("%d%d%d%d",&a,&b,&c,&d);
int ddd,fff;
ddd=b*d;
fff=a*d+b*c;
int ggg=gcd(ddd,fff);
cout<<fff/ggg<<" "<<ddd/ggg<<endl;
}
return 0;
}
本文通过一个简单的分数运算问题介绍了如何使用最大公约数算法来简化分数。代码采用C++实现,展示了输入两个分数并计算其相加结果的过程,同时利用最大公约数算法将结果分数进行最简化。
457

被折叠的 条评论
为什么被折叠?



