zoukankan      html  css  js  c++  java
  • [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 }
  • 相关阅读:
    jQuery EasyUI 详解
    The Google Test and Development Environment (持续更新)
    MTSC 2019 深圳站精彩议题第一波更新! | 七五折门票火热售票中
    MTSC2019-深圳站 议题征集
    [ZZ] [精彩盘点] TesterHome 社区 2018年 度精华帖
    MTSC2018 | 确认过眼神,在这里能遇见Google、阿里、百度......
    四岁啦 | 第四届中国移动互联网测试大会期待您的光临
    聊聊优酷上的那些车评
    聊聊Google DSM产品的发布
    尝试 Markdown 写测试用例
  • 原文地址:https://www.cnblogs.com/PureJava/p/10498125.html
Copyright © 2011-2022 走看看