[PAT] 1081 Rational Sum (20 分)Java

Posted Pure_Java

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[PAT] 1081 Rational Sum (20 分)Java相关的知识,希望对你有一定的参考价值。

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


 1 package pattest;
 2 
 3 import java.io.BufferedReader;
 4 import java.io.IOException;
 5 import java.io.InputStreamReader;
 6 
 7 /**
 8  * @Auther: Xingzheng Wang
 9  * @Date: 2019/2/26 21:26
10  * @Description: pattest
11  * @Version: 1.0
12  */
13 public class PAT1081 {
14     public static void main(String[] args) throws IOException {
15         BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
16         String n = reader.readLine();
17         String[] s1 = reader.readLine().split(" ");
18         String[] s2 = s1[0].split("/");
19         int a = Integer.parseInt(s2[0]), b = Integer.parseInt(s2[1]), sum = 0;
20         for (int i = 1; i < Integer.parseInt(n); i++) {
21             String[] s = s1[i].split("/");
22             int atemp = Integer.parseInt(s[0]);
23             int btemp = Integer.parseInt(s[1]);
24             a = a * btemp + atemp * b;
25             b = b * btemp;
26             int c = GCD(a, b);
27             a = a / c;
28             b = b / c;
29         }
30         if (a % b == 0) {
31             System.out.print(a / b);
32         } else {
33             if (Math.abs(a) > Math.abs(b)) {
34                 if (a > 0) {
35                     System.out.printf("%d %d/%d", a / b, a - a / b * b, b);
36                 } else {
37                     a = -a;
38                     System.out.printf("-%d %d/%d", a / b, a - a / b * b, b);
39                 }
40             } else {
41                 System.out.printf("%d/%d", a - a / b * b, b);
42             }
43         }
44     }
45 
46     private static int GCD(int m, int n) {
47         return n == 0 ? m : GCD(n, m % n);
48     }
49 }

 

以上是关于[PAT] 1081 Rational Sum (20 分)Java的主要内容,如果未能解决你的问题,请参考以下文章

[PAT] 1081 Rational Sum (20 分)Java

1081. Rational Sum (20)模拟——PAT (Advanced Level) Practise

PAT 甲级 1081 Rational Sum (数据不严谨 点名批评)

PAT甲级1081 Rational Sum (20 分)

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

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