2017 Java Chapter 13 (Week 13)

From: 2017-09-01 00:00:00 To: 2017-12-25 23:59:59 Now: 2024-11-23 17:15:59 Status: Public

C - 유리수 클래스1

Time Limit: 1s Memory Limit: 128MB

Snippet Judge Submissions: 793 Solved: 324
Description

분모와 분자를 BigInteger로 사용하는 Rational 클래스를 구현하시오.

(Use BigInteger for the Rational class) Redesign and implement the Rational class in Listing 13.13 using BigInteger for the numerator and denominator.

Input

* Line 1 : 테스트케이스의 개수 N

* Line 2 ~ N+1 : 각 케이스 별 숫자 a b c d

Output

* Line 1 ~ 4N : 각 테스트 케이스마다 다음과 같이 4줄씩 출력

- Line 1 : a/b + c/d = 결과

- Line 2 : a/b - c/d = 결과

- Line 3 : a/b * c/d = 결과

- Line 4 : a/b / c/d = 결과

- 모든 숫자는 분수 형태의 유리수

Sample Code
import java.math.*;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int T = sc.nextInt();
        for (int t = 0; t < T; t++) {
            Rational r1 = new Rational(new BigInteger(sc.next()), new BigInteger(sc.next()));
            Rational r2 = new Rational(new BigInteger(sc.next()), new BigInteger(sc.next()));

            System.out.println(r1 + " + " + r2 + " = " + r1.add(r2));
            System.out.println(r1 + " - " + r2 + " = " + r1.subtract(r2));
            System.out.println(r1 + " * " + r2 + " = " + r1.multiply(r2));
            System.out.println(r1 + " / " + r2 + " = " + r1.divide(r2));
        }
    }
}

YOUR_CODE
Sample Input
3
5 9 1 -6
-3 -9 -8 -2
-7 3 -6 -3
Sample Output
5/9 + -1/6 = 7/18
5/9 - -1/6 = 13/18
5/9 * -1/6 = -5/54
5/9 / -1/6 = -10/3
1/3 + 4 = 13/3
1/3 - 4 = -11/3
1/3 * 4 = 4/3
1/3 / 4 = 1/12
-7/3 + 2 = -1/3
-7/3 - 2 = -13/3
-7/3 * 2 = -14/3
-7/3 / 2 = -7/6
Source

JAVA2015 PE13.15