【PTA-1081】有理数的和
分析:有理数的运算
#include<iostream>
using namespace std;
typedef long long LL;
LL gcd(LL a,LL b)
{
return b?gcd(b,a%b):a;
}
int main()
{
int n;
cin>>n;
LL a = 0,b=1;
for(int i=0;i<n;i++){
LL c,d;
scanf("%lld/%lld",&c,&d);
int t = gcd(c,d);
c/=t,d/=t;
t = gcd(b,d);
// a/b+c/d = d/t*a+b/t*c // b*d/t;
a = d/t*a + b/t*c;
b = b/t*d;
t = gcd(a,b);
a/=t,b/=t;
}
if(b==1) printf("%lld",a);
else{
if(a>=b) printf("%lld ",a/b),a%=b;
printf("%lld/%lld",a,b);
}
return 0;
}