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

分析:先根据分数加法的公式累加,后分离出整数部分和分数部分
分子和分母都在长整型内,所以不能用int存储,否则有一个测试点不通过
一开始一直是浮点错误,按理来说应该是出现了/0或者%0的情况,找了半天也不知道错在哪里
后来注意到应该在累加的时候考虑是否会超出long long的范围,所以在累加每一步之前进行分子分母的约分处理,然后就AC了~
以及:abs()在stdlib.h头文件里面

应该还要考虑整数和小数部分都为0时候输出0的情况,但是测试用例中不涉及,所以如果没有最后两句也是可以AC的(PS:据说更新后的系统已经需要考虑全零的情况了~)

#include <iostream>
#include <cstdlib>
using namespace std;
long long gcd(long long a, long long b) {return b == 0 ? abs(a) : gcd(b, a % b);}
int main() {long long n, a, b, suma = 0, sumb = 1, gcdvalue;scanf("%lld", &n);for(int i = 0; i < n; i++) {scanf("%lld/%lld", &a, &b);gcdvalue = gcd(a, b);a = a / gcdvalue;b = b / gcdvalue;suma = a * sumb + suma * b;sumb = b * sumb;gcdvalue = gcd(suma, sumb);sumb = sumb / gcdvalue;suma = suma / gcdvalue;}long long integer = suma / sumb;suma = suma - (sumb * integer);if(integer != 0) {printf("%lld", integer);if(suma != 0) printf(" ");}if(suma != 0)printf("%lld/%lld", suma, sumb);if(integer == 0 && suma == 0)printf("0");return 0;
}

1081. Rational Sum (20)-PAT甲级真题相关推荐

  1. 1120. Friend Numbers (20)-PAT甲级真题

    1120. Friend Numbers (20) Two integers are called "friend numbers" if they share the same ...

  2. 1007. Maximum Subsequence Sum (25)-PAT甲级真题(最大连续子序列和、动态规划dp)

    Given a sequence of K integers { N1, N2, -, NK }. A continuous subsequence is defined to be { Ni, Ni ...

  3. 1042. Shuffling Machine (20)-PAT甲级真题

    Shuffling is a procedure used to randomize a deck of playing cards. Because standard shuffling techn ...

  4. 1108. Finding Average (20)-PAT甲级真题

    The basic task is simple: given N real numbers, you are supposed to calculate their average. But wha ...

  5. 1077. Kuchiguse (20)-PAT甲级真题

    The Japanese language is notorious for its sentence ending particles. Personal preference of such pa ...

  6. 1031. Hello World for U (20)-PAT甲级真题

    Given any string of N (>=5) characters, you are asked to form the characters into the shape of U. ...

  7. 1061. Dating (20)-PAT甲级真题

    Sherlock Holmes received a note with some strange strings: "Let's date! 3485djDkxh4hhGE 2984akD ...

  8. PAT甲级真题目录(按题型整理)(转自柳神)

    转载自:https://www.liuchuo.net/archives/2502?tdsourcetag=s_pcqq_aiomsg 最短路径 1003. Emergency (25)-PAT甲级真 ...

  9. PAT甲级真题 1018 A+B in Hogwarts--python解法

    PAT甲级真题 1018 A+B in Hogwarts 提交:2638 通过:1559 通过率:59% If you are a fan of Harry Potter, you would kno ...

最新文章

  1. python详细安装教程3.7.4-python 3.7.4下载与安装的问题
  2. 花生增产万书波谋定中国农民丰收节交易会 山东科技最高奖
  3. java8使用stream对List列表分组
  4. 【NLP】情感分析实战:金融市场中的NLP
  5. sklearn自学指南(part21)--核岭回归
  6. P4149-[IOI2011]Race【点分治】
  7. 数据库原理及应用【一】引言
  8. boundcolumn 根据值进行判断_Excel使用函数进行条件判断的方法步骤
  9. 【OpenCV 例程200篇】46. 直方图均衡化
  10. CRF(条件随机场) 学习总结
  11. 学会使用 GDB 调试 Go 代码
  12. visual studio installer 卸载时不能删除安装目录问题
  13. Bailian3247 回文素数【素数+回文】(POJ NOI0113-11)
  14. 中英文对照 —— 缩略词
  15. QSS按钮样式设置的坑,按下(pressed)时按钮颜色不变化
  16. linux锐捷认证成功无法上网,锐捷认证成功但是却无法上网怎么办
  17. js中base64编码
  18. 纯HTML+CSS+js实现大型企业站小米商城官网之注册页面
  19. Twilight暮光之城。。。暮色。。。
  20. 2022年施工升降机司机(建筑特殊工种)考题及答案

热门文章

  1. Google Map 附近查询
  2. PHP:header()函数
  3. Microsoft Project 2010
  4. 如何修改Solaris的时区信息
  5. myeclipse 中的'ISO-8859-1'编码问题
  6. C#判断当前运行环境是否64bit
  7. 2017-10-7Linux基础知识(5)基本命令
  8. 解决ios下的微信打开的页面背景音乐无法自动播放(转载)
  9. fopen 參数具体解释
  10. encodeURIcomponent编码和ASP.NET之间编码转换