PAT乙級——1001 A+B和C java實現

題目描述

給定區間[-2的31次方, 2的31次方]內的3個整數A、B和C,請判斷A+B是否大於C。

輸入描述:

輸入第1行給出正整數T(<=10),是測試用例的個數。隨後給出T組測試用例,每組佔一行,順序給出A、B和C。整數間以空格分隔。


輸出描述:

對每組測試用例,在一行中輸出“Case #X: true”如果A+B>C,否則輸出“Case #X: false”,其中X是測試用例的編號(從1開始)。

輸入例子:

4

1 2 3

2 3 4

2147483647 0 2147483646

0 -2147483648 -2147483647

輸出例子:

Case #1: false

Case #2: true

Case #3: true

Case #4: false

因爲java裏自帶BigInteger所以直接用即可

下面是代碼

import java.math.BigInteger;
import java.util.Scanner;

public class Main{
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		int n = scanner.nextInt();
		for (int i = 1; i <= n; i++) {
			BigInteger a = scanner.nextBigInteger();
			BigInteger b = scanner.nextBigInteger();
			BigInteger c = scanner.nextBigInteger();
			if (a.add(b).compareTo(c) > 0 ) {
				System.out.println("Case #" + i + ": true");
			} else
				System.out.println("Case #" + i + ": false");
		}
		scanner.close();
	}
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章