题目描述

  1. Given N rational numbers in the form "numerator/denominator", you are supposed to calculate their sum.

输入描述:

  1. 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.

输出描述:

  1. 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.

输入例子:

  1. 5
  2. 2/5 4/15 1/30 -2/60 8/3

输出例子:

  1. 3 1/3
  1. #include <iostream>
  2. #include <vector>
  3. #include <math.h>
  4.  
  5. using namespace std;
  6.  
  7. long int gcd(long int a, long int b)
  8. {
  9. if(b==) return a;
  10. else return gcd(b,a%b);
  11. }
  12.  
  13. int main()
  14. {
  15. double N;
  16. cin>>N;
  17. long int Inter = ;//整数
  18. long int resa = ;//答案分子
  19. long int resb = ;//答案分母
  20.  
  21. for(int i=;i<N;++i)
  22. {
  23. long int a = ;//输入分子
  24. long int b = ;//输入分母
  25. char c;
  26. cin >> a >> c >> b;
  27.  
  28. int f = ;
  29. if(a<)
  30. {
  31. a = a*-;
  32. f = -;
  33. }
  34. Inter += a/b; //简化
  35. a = a-b*(a/b);
  36.  
  37. long int Div = ;//最大公约数
  38. long int Mul = ;//最小公倍数
  39. //化简输入的分数
  40. Div = gcd(b,a);
  41. a = a/Div;
  42. b = b/Div;
  43.  
  44. //求最大公倍数
  45. if(resb > b)
  46. {
  47. Div = gcd(resb,b);
  48. Mul = resb / Div * b;
  49. }
  50. else
  51. {
  52. Div = gcd(b, resb);
  53. Mul = b / Div * resb;
  54. }
  55. //相加
  56. resa = resa * (Mul/resb) + f * a * (Mul/b);
  57. resb = Mul;
  58. //化简有理数
  59. Inter += resa / resb;
  60. resa = resa - (resa / resb)*resb;
  61.  
  62. //化简最简分数
  63. Div = gcd(resb, fabs(resa));
  64. resa = resa/Div;
  65. resb = resb/Div;
  66. }
  67. if(Inter== && resa==)
  68. cout << << endl;
  69. else if(Inter != && resa == )
  70. cout << Inter << endl;
  71. else if(Inter == && resa != )
  72. cout << resa << "/" << resb << endl;
  73. else
  74. cout << Inter << " " << resa << "/" << resb << endl;
  75.  
  76. return ;
  77. }
  1.  

PAT甲级——【牛客练习题100】的更多相关文章

  1. PAT甲级——【牛客练习题1002】

    题目描述 Given an integer with no more than 9 digits, you are supposed to read it in the traditional Chi ...

  2. PAT甲级训练刷题代码记录

    刷题链接:https://www.patest.cn/contests/pat-a-practise 1001 #include <iostream> #include <stdio ...

  3. PAT甲级题解(慢慢刷中)

    博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6102219.html特别不喜欢那些随便转载别人的原创文章又不给 ...

  4. PAT甲级1131. Subway Map

    PAT甲级1131. Subway Map 题意: 在大城市,地铁系统对访客总是看起来很复杂.给你一些感觉,下图显示了北京地铁的地图.现在你应该帮助人们掌握你的电脑技能!鉴于您的用户的起始位置,您的任 ...

  5. PAT——甲级1009:Product of Polynomials;乙级1041:考试座位号;乙级1004:成绩排名

    题目 1009 Product of Polynomials (25 point(s)) This time, you are supposed to find A×B where A and B a ...

  6. PAT甲级考前整理(2019年3月备考)之一

       转载请注明出处:https://www.cnblogs.com/jlyg/p/7525244.html 终于在考前,刷完PAT甲级131道题目,不容易!!!每天沉迷在刷题之中而不能超脱,也是一种 ...

  7. PAT甲级终结——心得总结

    首先报喜一波 第一次考,满分,4道题总共花了2个小时做完,一部分是题简单的原因,一部分也是自己三刷了PAT的心血吧. 刷PAT的经验 神指导: 胡凡-<算法笔记> 神助攻:柳婼的博客,百度 ...

  8. 1080 Graduate Admission——PAT甲级真题

    1080 Graduate Admission--PAT甲级练习题 It is said that in 2013, there were about 100 graduate schools rea ...

  9. C++版 - HDUoj 2010 3阶的水仙花数 - 牛客网

    版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C++版 - ...

随机推荐

  1. Neo4j使用简单例子

    Neo4j Versions Most of the examples on this page are written with Neo4j 2.0 in mind, so they skip th ...

  2. Ajax.BeginForm 在 Chrome下的问题

    项目背景:MVC4 代码: @using (Ajax.BeginForm("Index", "GoingMeter", new AjaxOptions { On ...

  3. 无法解析的外部符号 jpeg_std_error

    1>dlib.lib(png_loader.obj) : error LNK2001: 无法解析的外部符号 png_set_sig_bytes 1>dlib.lib(png_loader. ...

  4. 清空资源管理器访问过FTP的账号、密码

    修改注册表,删除HKEY_CURRENT_USER\SOFTWARE\Microsoft\FTP\Accounts下相对应的项即可,即为xxx.xxx.xxx.xxx项. 如下图所示:

  5. JS鼠标经过

    <!DOCTYPE html><html><head> <meta charset="utf-8"> <title>菜鸟 ...

  6. Python 正整数相加其余忽略

    从键盘上输入若干数值,对其中的正整数求和,非正整数(负整数,实数或其他符号)忽略,这个过程一直到输入“#”结束. i = 0while True: m = input("请输入一个数:&qu ...

  7. python 基本常用数据类型

    #字典类型 result={1:2222,2:2221111}; result.items();#获取字典中所有元素 result.keys();#获取字典的key result.values();# ...

  8. 括号匹配——cf1095E

    正解应该是求后缀和前缀 但是多情况讨论好像也能过.. 大概分为: 首先排除不能改的情况 1.改左括号 2.改右括号 /* 将一个位置的括号反过来,使原序列变成合法序列 */ #include<b ...

  9. android—退出应用程序

    在android系统中,当你点击返回按钮时,会默认调用finish方法(还是destroy方法,记不太清楚),这样你就能退出当前Activity.注意是当前Activity,不是应用程序,因为如果这个 ...

  10. centos7 创建桌面快捷方式(chrome,eclipse)

    在将eclipse-SDK-3.7.2-Linux-gtk.tar.gz解压到某个目录下之后,命令行进行如下编辑 vi /usr/share/applications/eclipse.desktop ...