1081. Rational Sum (20)【模拟】——PAT (Advanced Level) Practise
来源:互联网 发布:国云数据裁员 编辑:程序博客网 时间:2024/05/21 16:57
题目信息
1081. Rational Sum (20)
时间限制400 ms
内存限制65536 kB
代码长度限制16000 B
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
解题思路
模拟加法,注意结果中分母负号和分子为0情况
AC代码
#include <cstdio>struct node{ long long a, b; node(long long a, long long b):a(a), b(b){}};long long gcd(long long a, long long b){ return b ? gcd(b, a%b) : a;}node add(node& a, node& b){ node r(a.a*b.b + a.b*b.a, a.b*b.b); long long t = gcd(r.a, r.b); r.a /= t; r.b /= t; return r;}int main(){ int n; long long a, b; scanf("%d", &n); scanf("%lld/%lld", &a, &b); node p(a, b); for (int i = 1; i < n; ++i){ scanf("%lld/%lld", &a, &b); node t(a, b); p = add(p, t); } if (p.b < 0){ p.b *= -1; p.a *= -1; } if (p.a >= p.b){ printf("%lld", p.a/p.b); if (p.a % p.b){ printf(" %lld/%lld", p.a%p.b, p.b); } }else{ if (p.a == 0){ printf("0"); }else{ printf("%lld/%lld", p.a, p.b); } } printf("\n"); return 0;}
- 1081. Rational Sum (20)【模拟】——PAT (Advanced Level) Practise
- 1088. Rational Arithmetic (20)——PAT (Advanced Level) Practise
- PAT (Advanced Level) Practise 1081 Rational Sum (20)
- PAT (Advanced Level) Practise 1081 Rational Sum(20)
- PAT Advanced Level 1081. Rational Sum (20)
- 【PAT】【Advanced Level】1081. Rational Sum (20)
- PAT (Advanced Level) Practise 1088 Rational Arithmetic (20)
- PAT (Advanced Level) Practise 1088 Rational Arithmetic (20)
- 1104. Sum of Number Segments (20)【数学题】——PAT (Advanced Level) Practise
- 1042. Shuffling Machine (20)【模拟】——PAT (Advanced Level) Practise
- 1069. The Black Hole of Numbers (20)【模拟】——PAT (Advanced Level) Practise
- PAT (Advanced Level) Practise 1104 Sum of Number Segments (20)
- PAT (Advanced Level) Practise 1104 Sum of Number Segments (20)
- PAT (Advanced Level) Practise —— 1015. Reversible Primes (20)
- 1008. Elevator (20)——PAT (Advanced Level) Practise
- 1015. Reversible Primes (20) ——PAT (Advanced Level) Practise
- 1096. Consecutive Factors (20)——PAT (Advanced Level) Practise
- [PAT (Advanced Level) ]1081. Rational Sum 解题文档
- 【bzoj2588】Spoj 10628. Count on a tree LCA+主席树
- 局部变量的默认值是什么?默认值在Debug版本和Release版本的区别?0XCCCCCCCC是什么
- android_定位的介绍
- The Network Adapter could not establish the connection at oracle.jdbc.driver.T4CConnection.logon
- 【个人】我与机房的初♂夜
- 1081. Rational Sum (20)【模拟】——PAT (Advanced Level) Practise
- 最小生成树kruskal
- mongodb调优那些事(一)-系统设置
- Spinner报错: java.lang.RuntimeException: setOnItemClickListener cannot be used with a spinner.
- java 找出n个元素数组中重复次数最多的数(假设出现次数大于n/2)
- 1082. Read Number in Chinese (25)【字符串处理】——PAT (Advanced Level) Practise
- 1083. List Grades (25)【水题】——PAT (Advanced Level) Practise
- linux epoll 简介
- 易互娱2017实习生招聘在线笔试第一场题目1 : 电子数字