https://pintia.cn/problem-sets/994805342720868352/problems/994805386161274880

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:

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

Sample Output 1:

  1. 3 1/3

Sample Input 2:

  1. 2
  2. 4/3 2/3

Sample Output 2:

  1. 2

Sample Input 3:

  1. 3
  2. 1/3 -1/6 1/8

Sample Output 3:

  1. 7/24
  2.  
  3. 代码:
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. long long a[111], b[111];
  5. long long sum, m;
  6.  
  7. long long gcd(long long x, long long y) {
  8. long long z = x % y;
  9. while(z) {
  10. x = y;
  11. y = z;
  12. z = x % y;
  13. }
  14. return y;
  15. }
  16.  
  17. long long ad(long long x, long long y) {
  18. if(x > y)
  19. swap(x, y);
  20. if(y % x == 0)
  21. return y;
  22. else
  23. return x * y / gcd(x, y);
  24. }
  25.  
  26. void display(long long p, long long q) {
  27. if(q == 0 || p == 0)
  28. printf("0\n");
  29. else {
  30. bool flag = true;
  31. if(p < 0) {
  32. flag = false;
  33. printf("-");
  34. p = abs(p);
  35. }
  36.  
  37. if(p / q != 0) {
  38. if(p % q == 0)
  39. printf("%lld\n", p / q);
  40. else {
  41. long long mm = p / q;
  42. printf("%lld ", mm);
  43. if(!flag) cout << "-";
  44. printf("%lld/%lld", (p - mm * q) / gcd(p - mm * q, q), q / gcd(p - mm * q, q));
  45. }
  46. } else {
  47. printf("%lld/%lld", p / gcd(p, q), q / gcd(p, q));
  48. }
  49.  
  50. }
  51. }
  52.  
  53. void add(long long x, long long y) {
  54. // sum / m + x / y
  55. // = (sum * y + m * x) / (x * y);
  56. long long xx = sum * y + m * x;
  57. long long yy = m * y;
  58. long long g = gcd(abs(xx), abs(yy));
  59. xx /= g;
  60. yy /= g;
  61. sum = xx;
  62. m = yy;
  63. }
  64.  
  65. int main() {
  66.  
  67. int N;
  68. scanf("%d", &N);
  69. for(int i = 1; i <= N; i ++)
  70. scanf("%lld/%lld", &a[i], &b[i]);
  71.  
  72. if(N == 0) {
  73. printf("0\n");
  74. return 0;
  75. }
  76. if(N ==1) {
  77. display(a[1], b[1]);
  78. return 0;
  79. }
  80.  
  81. /*
  82. long long m = ad(b[1], b[2]);
  83. for(int i = 3; i <= N; i ++) {
  84. m = ad(m, b[i]);
  85. }
  86.  
  87. long long sum = 0;
  88. for(int i = 1; i <= N; i ++) {
  89. sum += a[i] * m / b[i];
  90. }
  91. */
  92. sum = a[1];
  93. m = b[1];
  94. for(int i = 2; i <= N; i ++) {
  95. add(a[i], b[i]);
  96. }
  97.  
  98. if(m < 0) {
  99. sum = -sum;
  100. m = -m;
  101. }
  102. display(sum, m);
  103.  
  104. return 0;
  105. }

  

PAT 甲级 1081 Rational Sum (数据不严谨 点名批评)的更多相关文章

  1. PAT Advanced 1081 Rational Sum (20) [数学问题-分数的四则运算]

    题目 Given N rational numbers in the form "numerator/denominator", you are supposed to calcu ...

  2. PAT甲级——A1081 Rational Sum

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

  3. PAT 1081 Rational Sum

    1081 Rational Sum (20 分)   Given N rational numbers in the form numerator/denominator, you are suppo ...

  4. PAT 1081 Rational Sum[分子求和][比较]

    1081 Rational Sum (20 分) Given N rational numbers in the form numerator/denominator, you are suppose ...

  5. 【PAT甲级】1081 Rational Sum (20 分)

    题意: 输入一个正整数N(<=100),接着输入N个由两个整数和一个/组成的分数.输出N个分数的和. AAAAAccepted code: #define HAVE_STRUCT_TIMESPE ...

  6. PAT甲题题解-1081. Rational Sum (20)-模拟分数计算

    模拟计算一些分数的和,结果以带分数的形式输出注意一些细节即可 #include <iostream> #include <cstdio> #include <algori ...

  7. PAT (Advanced Level) 1081. Rational Sum (20)

    简单模拟题. #include<cstdio> #include<cstring> #include<cmath> #include<vector> # ...

  8. 1081. Rational Sum (20)

    the problem is from PAT,which website is http://pat.zju.edu.cn/contests/pat-a-practise/1081 the code ...

  9. 1081. Rational Sum (20) -最大公约数

    题目如下: Given N rational numbers in the form "numerator/denominator", you are supposed to ca ...

随机推荐

  1. unix文件共享

    UNIX系统支持在不同的进程间共享打开文件.内核使用3种数据结构表示打开文件,他们之间的关系决定了在文件共享方面一个进程对另一个进程产生的影响. (1)每个进程在进程表中都有一个记录项,记录项中包含一 ...

  2. Scala数组操作

    数组操作 初始化固定长度的数组 // 初始化长度为10的数组 val array = new Array[Int](10) // 初始化创建含有hello与Scala的数组 val s = Array ...

  3. 2014年第五届蓝桥杯B组(C/C++)预赛题目及个人答案(欢迎指正)

    参考:https://blog.csdn.net/qq_30076791/article/details/50573512 第3题: #include<bits/stdc++.h> usi ...

  4. 20155203 2016-2017-2 《Java程序设计》第10周学习总结

    20155203 2016-2017-2 <Java程序设计>第10周学习总结 教材学习内容总结 网络编程(Java Socket编程) Java最初是作为网络编程语言出现的,其对网络提供 ...

  5. 20155234java实验一

    实验内容 1.使用JDK编译.运行简单的Java程序: 2.使用Eclipse 编辑.编译.运行.调试Java程序. 实验要求 1.没有Linux基础的同学建议先学习Linux基础入门(新版))Vim ...

  6. 同步备份工具之 rsync

    1.常用同步方法 SCP. NFS. SFTP. http. samba. rsync. drbd(基于文件系统同步,效率高) 2.rsync 介绍 rsync,英文全称是 remote synchr ...

  7. C#:设置CefSharp的一些参数,比如忽略安全证书

    ------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- 本次记录如何设置CefSharp忽略安全证书,以及他的一些其他配置 参考网址: https://peter.s ...

  8. Retinex图像增强和暗通道去雾的关系及其在hdr色调恢复上的应用

    很多人都认为retinex和暗通道去雾是八杆子都打不着的增强算法.的确,二者的理论.计算方法都完全迥异,本人直接从二者的公式入手来简单说明一下,有些部分全凭臆想,不对之处大家一起讨论. 首先,为描述方 ...

  9. Intellij IDEA 2017 通过scala工程运行wordcount

    首先是安装scala插件,可以通过idea内置的自动安装方式进行,也可以手动下载可用的插件包之后再通过idea导入. scala插件安装完成之后,新建scala项目,右侧使用默认的sbt 点击Next ...

  10. javaweb(三十一)——国际化(i18n)

    一.国际化开发概述 软件的国际化:软件开发时,要使它能同时应对世界不同地区和国家的访问,并针对不同地区和国家的访问,提供相应的.符合来访者阅读习惯的页面或数据. 国际化(internationaliz ...