[PAT] 1088 Rational Arithmetic (20 分)Java

For two rational numbers, your task is to implement the basic arithmetics, that is, to calculate their sum, difference, product and quotient.

Input Specification:

Each input file contains one test case, which gives in one line the two rational numbers in the format a1/b1 a2/b2. The numerators and the denominators are all in the range of long int. If there is a negative sign, it must appear only in front of the numerator. The denominators are guaranteed to be non-zero numbers.

Output Specification:

For each test case, print in 4 lines the sum, difference, product and quotient of the two rational numbers, respectively. The format of each line is number1 operator number2 = result. Notice that all the rational numbers must be in their simplest form k a/b, where k is the integer part, and a/b is the simplest fraction part. If the number is negative, it must be included in a pair of parentheses. If the denominator in the division is zero, output Inf as the result. It is guaranteed that all the output integers are in the range of long int.

Sample Input 1:

2/3 -4/2

Sample Output 1:

2/3 + (-2) = (-1 1/3)
2/3 - (-2) = 2 2/3
2/3 * (-2) = (-1 1/3)
2/3 / (-2) = (-1/3)

Sample Input 2:

5/3 0/6

Sample Output 2:

1 2/3 + 0 = 1 2/3
1 2/3 - 0 = 1 2/3
1 2/3 * 0 = 0
1 2/3 / 0 = Inf

  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/27 12:11
 10  * @Description: pattest
 11  * @Version: 2.0
 12  */
 13 public class PAT1088 {
 14     public static void main(String[] args) throws IOException {
 15         BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
 16         String[] s1 = reader.readLine().split(" ");
 17         String[] s2 = s1[0].split("/");
 18         String[] s3 = s1[1].split("/");
 19         int a1 = Integer.parseInt(s2[0]);
 20         int b1 = Integer.parseInt(s2[1]);
 21         int a2 = Integer.parseInt(s3[0]);
 22         int b2 = Integer.parseInt(s3[1]);
 23         int c1 = GCD(a1, b1);
 24         int c2 = GCD(a2, b2);
 25         a1 = a1 / c1;
 26         b1 = b1 / c1;
 27         a2 = a2 / c2;
 28         b2 = b2 / c2;
 29 //        System.out.printf("%d,%d,%d,%d,%d,%d",a1,b1,c1,a2,b2,c2);//2,3,1,-2,1,2
 30         sum(a1, b1, a2, b2);
 31         System.out.println();
 32         difference(a1, b1, a2, b2);
 33         System.out.println();
 34         product(a1, b1, a2, b2);
 35         System.out.println();
 36         quotient(a1, b1, a2, b2);
 37     }
 38 
 39     private static void sum(int m1, int n1, int m2, int n2) {
 40         int mtemp = m1 * n2 + m2 * n1;
 41         int ntemp = n1 * n2;
 42         printSingleNum(m1, n1);
 43         System.out.print(" + ");
 44         printSecondNumAndResult(m2, n2, mtemp, ntemp);
 45     }
 46 
 47     private static void difference(int m1, int n1, int m2, int n2) {
 48         int mtemp = m1 * n2 - m2 * n1;
 49         int ntemp = n1 * n2;
 50         printSingleNum(m1, n1);
 51         System.out.print(" - ");
 52         printSecondNumAndResult(m2, n2, mtemp, ntemp);
 53     }
 54 
 55     private static void product(int m1, int n1, int m2, int n2) {
 56         int mtemp = m1 * m2;
 57         int ntemp = n1 * n2;
 58         if (m1 == 0) {
 59             System.out.print("0 * ");
 60             printSingleNum(m2, n2);
 61             System.out.print(" = 0");
 62         }
 63         if (m2 == 0) {
 64             printSingleNum(m1, n1);
 65             System.out.print(" * 0 = 0");
 66         } else {
 67             printSingleNum(m1, n1);
 68             System.out.print(" * ");
 69             printSecondNumAndResult(m2, n2, mtemp, ntemp);
 70         }
 71     }
 72 
 73     private static void quotient(int m1, int n1, int m2, int n2) {
 74         if (m2 != 0) {
 75             int mtemp = m1 * n2;
 76             int ntemp = m2 * n1;
 77             if (ntemp < 0) {
 78                 mtemp = -mtemp;
 79                 ntemp = -ntemp;
 80             }
 81             printSingleNum(m1, n1);
 82             System.out.print(" / ");
 83             printSecondNumAndResult(m2, n2, mtemp, ntemp);
 84         } else {
 85             printSingleNum(m1, n1);
 86             System.out.print(" / 0 = Inf");
 87         }
 88     }
 89 
 90     private static void printSingleNum(int m, int n) {
 91         if (m % n == 0) {
 92             if (m >= 0) {
 93                 System.out.print(m / n);
 94             } else {
 95                 System.out.printf("(%d)", m / n);
 96             }
 97         } else {
 98             if (Math.abs(m) > Math.abs(n)) {
 99                 if (m >= 0) {
100                     System.out.printf("%d %d/%d", m / n, m - m / n * n, n);
101                 } else {
102                     m = -m;
103                     System.out.printf("(-%d %d/%d)", m / n, m - m / n * n, n);
104                 }
105             } else {
106                 if (m >= 0) {
107                     System.out.printf("%d/%d", m - m / n * n, n);
108                 } else {
109                     System.out.printf("(%d/%d)", m - m / n * n, n);
110                 }
111             }
112         }
113     }
114 
115     private static void printSecondNumAndResult(int m, int n, int mtemp, int ntemp) {
116         printSingleNum(m, n);
117         System.out.print(" = ");
118         int ctemp = Math.abs(GCD(mtemp, ntemp));
119         printSingleNum(mtemp / ctemp, ntemp / ctemp);
120     }
121 
122     private static int GCD(int m, int n) {
123         return n == 0 ? m : GCD(n, m % n);
124     }
125 }

猜你喜欢

转载自www.cnblogs.com/PureJava/p/10498211.html
今日推荐