题意:
输入一个正整数N(<=100),接着输入N个由两个整数和一个/组成的分数。输出N个分数的和。
AAAAAccepted code:
1 #define HAVE_STRUCT_TIMESPEC 2 #include<bits/stdc++.h> 3 using namespace std; 4 int a[107],b[107]; 5 int main(){ 6 ios::sync_with_stdio(false); 7 cin.tie(NULL); 8 cout.tie(NULL); 9 int n; 10 cin>>n; 11 for(int i=1;i<=n;++i){ 12 cin>>a[i]; 13 cin.ignore(); 14 cin>>b[i]; 15 int t=max(1,__gcd(a[i],b[i])); 16 a[i]/=t; 17 b[i]/=t; 18 } 19 int ans=0; 20 for(int i=2;i<=n;++i){ 21 int tamp=b[i-1]*b[i]; 22 int sum=a[i]*b[i-1]+a[i-1]*b[i]; 23 int t=__gcd(tamp,sum); 24 tamp/=t; 25 sum/=t; 26 ans+=sum/tamp; 27 sum%=tamp; 28 a[i]=sum; 29 b[i]=tamp; 30 } 31 if(ans&&a[n]) 32 cout<<ans<<" "<<a[n]<<"/"<<b[n]; 33 else if(ans) 34 cout<<ans; 35 else if(a[n]) 36 cout<<a[n]<<"/"<<b[n]; 37 else 38 cout<<0; 39 return 0; 40 }