PAT 1081. Rational Sum (20)
2014-08-27 16:31
495 查看
http://pat.zju.edu.cn/contests/pat-a-practise/1081
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:
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:
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
题目难度不大,但是这种题目真的是讨厌,各种小问题,要反复调试。
#include <cstdio> long long gcd(long long x, long long y){ if (y == 0) return x; else return gcd(y, x%y); } long long lcm(long long x, long long y){ return x / gcd(x, y) *y; } long long a[100], b[100]; int main(){ int n; scanf("%d", &n); for (int i = 0; i < n; i++){ scanf("%lld/%lld", &a[i], &b[i]); } long long deno = 1, nume = 0; for (int i = 0; i < n; i++) deno = lcm(deno, b[i]); for (int i = 0; i < n; i++) nume += deno / b[i] * a[i]; long long pre = nume / deno; nume = nume - pre*deno; long long t = gcd(nume, deno); nume = nume / t; deno = deno / t; if (pre == 0 && nume == 0) printf("0\n"); else if (pre != 0 && nume == 0) printf("%lld\n", pre); else if (pre == 0 && nume != 0) printf("%lld/%lld\n", nume, deno); else printf("%lld %lld/%lld\n", pre, nume, deno); return 0; }
相关文章推荐
- 1081. Rational Sum (20)-PAT甲级真题
- PAT (Advanced Level) 1081. Rational Sum (20)
- 1081. Rational Sum (20) PAT甲级
- PAT (Advanced) 1081. Rational Sum (20)
- PAT-A 1081. Rational Sum (20)
- PAT (Advanced) 1081. Rational Sum (20)
- 【PAT】1081. Rational Sum (20)
- PAT 1081. Rational Sum (20) GCD
- pat甲1081. Rational Sum (20)
- PAT 1081. Rational Sum (20)
- PAT 1081. Rational Sum (20)
- PAT 1081. Rational Sum (20)
- ZJU-PAT 1081. Rational Sum (20)
- PAT-A-1081. Rational Sum (20)
- PAT甲题题解-1081. Rational Sum (20)-模拟分数计算
- PAT (Advanced Level) Practise 1081 Rational Sum(20)
- PAT 1081. Rational Sum (20) 辗转相除法, 分数运算
- PAT (Advanced Level) 1081. Rational Sum (20) 分数求和,辗转相除法求gcd
- PAT 1081. Rational Sum(20)
- 【PAT】1081. Rational Sum (20)