题目
题目链接 PTA | 程序设计类实验辅助教学平台
AC答案1
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 = new BigInteger(scanner.next());
BigInteger B = new BigInteger(scanner.next());
BigInteger C = new BigInteger(scanner.next());
if ((A.add(B)).compareTo(C) > 0){
System.out.println("Case #"+i+": true");
}else {
System.out.println("Case #"+i+": false");
}
}
}
}
AC答案2
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++) {
long A = scanner.nextLong();
long B = scanner.nextLong();
long C = scanner.nextLong();
long sum = A + B;
if (A > 0 && B > 0 && sum < 0) {
//sum为负数,说明发生溢出,说明sum过大,但C仍在表示范围之内,所以sum比C大
System.out.println("Case #" + i + ": true");
} else if (A < 0 && B < 0 && sum > 0) {
//sum为正数,说明发生溢出,说明sum过小,但C仍在表示范围之内,所以sum比C小
System.out.println("Case #" + i + ": false");
} else if (sum > C) {
//并没有发生溢出
System.out.println("Case #" + i + ": true");
} else {
//并没有发生溢出
System.out.println("Case #" + i + ": false");
}
}
}
}