【PAT甲级】1081 Rational Sum (20 分)

时间:2021-04-20 17:47:22

题意:

输入一个正整数N(<=100),接着输入N个由两个整数和一个/组成的分数。输出N个分数的和。

AAAAAccepted code:

 #define HAVE_STRUCT_TIMESPEC
#include<bits/stdc++.h>
using namespace std;
int a[],b[];
int main(){
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n;
cin>>n;
for(int i=;i<=n;++i){
cin>>a[i];
cin.ignore();
cin>>b[i];
int t=max(,__gcd(a[i],b[i]));
a[i]/=t;
b[i]/=t;
}
int ans=;
for(int i=;i<=n;++i){
int tamp=b[i-]*b[i];
int sum=a[i]*b[i-]+a[i-]*b[i];
int t=__gcd(tamp,sum);
tamp/=t;
sum/=t;
ans+=sum/tamp;
sum%=tamp;
a[i]=sum;
b[i]=tamp;
}
if(ans&&a[n])
cout<<ans<<" "<<a[n]<<"/"<<b[n];
else if(ans)
cout<<ans;
else if(a[n])
cout<<a[n]<<"/"<<b[n];
else
cout<<;
return ;
}