ZJU-PAT 1081. Rational Sum (20)
2014-07-08 23:42
453 查看
Given N rational numbers in the form "numerator/denominator", you are supposed to calculate their sum.
Input Specification:
Each input file contains one test case. Each case starts with a positive integer N (<=100), followed in the next line N rational numbers "a1/b1 a2/b2 ..." where all the numerators and denominators are in the range of "long int". If there is a negative number,
then the sign must appear in front of the numerator.
Output Specification:
For each test case, output the sum in the simplest form "integer numerator/denominator" where "integer" is the integer part of the sum, "numerator" < "denominator", and the numerator and the denominator have no common factor. You must output only the fractional
part if the integer part is 0.
Sample Input 1:
Sample Output 1:
Sample Input 2:
Sample Output 2:
Sample Input 3:
Sample Output 3:
模拟题
Input Specification:
Each input file contains one test case. Each case starts with a positive integer N (<=100), followed in the next line N rational numbers "a1/b1 a2/b2 ..." where all the numerators and denominators are in the range of "long int". If there is a negative number,
then the sign must appear in front of the numerator.
Output Specification:
For each test case, output the sum in the simplest form "integer numerator/denominator" where "integer" is the integer part of the sum, "numerator" < "denominator", and the numerator and the denominator have no common factor. You must output only the fractional
part if the integer part is 0.
Sample Input 1:
5 2/5 4/15 1/30 -2/60 8/3
Sample Output 1:
3 1/3
Sample Input 2:
2 4/3 2/3
Sample Output 2:
2
Sample Input 3:
3 1/3 -1/6 1/8
Sample Output 3:
7/24
模拟题
#pragma warning (disable:4786) #include<iostream> #include<iomanip> #include<string> #include<cstdio> #include<algorithm> #include<map> #include<sstream> using namespace std; long long int sum,ans,sum1; int n; struct Node { long long int numerator,denominator; bool flag; }; Node TT[105]; long long int LCM(long long int a,long long int b) { while(b!=0) { long long int r=a%b; a=b; b=r; } return a; } void Jisuan() { ans=TT[0].numerator; sum=TT[0].denominator; for(int i=1;i<n;i++) { sum1=sum/LCM(sum,TT[i].denominator)*TT[i].denominator; long long int p=sum1/sum; long long int mm=sum1/TT[i].denominator; ans=ans*p; ans=ans+TT[i].numerator*mm; sum=sum1; } sum1=LCM(ans,sum); ans=ans/sum1; sum=sum/sum1; } int main() { while(cin>>n) { for(int i=0;i<n;i++) { scanf("%lld/%lld",&TT[i].numerator,&TT[i].denominator); if(TT[i].numerator<0) TT[i].flag=false; } Jisuan(); if(abs(ans)>sum) { int ii=ans/sum; int tt=ans%sum; cout<<ii; if(tt) cout<<" "<<tt<<"/"<<sum; cout<<endl; } else { if(ans) cout<<ans<<"/"<<sum<<endl; else cout<<"0"<<endl; } } return 0; }
相关文章推荐
- PAT (Advanced Level) 1081. Rational Sum (20) 分数求和,辗转相除法求gcd
- PAT甲题题解-1081. Rational Sum (20)-模拟分数计算
- PAT-A-1081. Rational Sum (20)
- PAT 1081. Rational Sum (20) GCD
- PAT 1081. Rational Sum(20)
- 1081. Rational Sum (20)-PAT甲级真题
- 【PAT】1081. Rational Sum (20)
- PAT 1081. Rational Sum (20)
- PAT 1081. Rational Sum (20)(分数加法)(待修改)
- PAT 1081. Rational Sum (20) 辗转相除法, 分数运算
- PAT (Advanced Level) 1081. Rational Sum (20)
- PAT-A 1081. Rational Sum (20)
- PAT (Advanced Level) Practise 1081 Rational Sum(20)
- PAT 1081. Rational Sum (20)
- 浙江大学PAT_甲级_1081. Rational Sum (20)
- PAT (Advanced) 1081. Rational Sum (20)
- [PAT]1081 Rational Sum (20)
- 【PAT】【Advanced Level】1081. Rational Sum (20)
- PAT Advanced Level 1081. Rational Sum (20)
- pat甲1081. Rational Sum (20)