PAT-乙级-Java-1011

1011 A+B 和 C (15 分)

给定区间 [−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代码实现:

import java.util.Scanner;

public class Main {

	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);
		int num = Integer.parseInt(sc.nextLine());
		boolean [] result = new boolean[num];
		for(int i = 1;i<num+1;i++){
			String input = sc.nextLine();
			//这道题需要注意的是题目中说了给定的区间是在-2^31~2^31,超出int范围,所以需要使用long型
			if(Long.parseLong(input.split("\\s+")[0])+Long.parseLong(input.split("\\s+")[1])>Long.parseLong(input.split("\\s+")[2])){
				result[i-1] = true;
			}else{
				result[i-1] = false;
			}
			
		}
		for(int i = 1;i<num+1;i++){
			System.out.println("Case #"+i+": "+result[i-1]);
		}
	}
}

猜你喜欢

转载自blog.csdn.net/QYHuiiQ/article/details/84035784