1011 A+B 和 C (15分)
給定區間 [−231,231] 內的 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
第一版:
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 = 0; i < n; i++) {
long nextLong = scanner.nextLong();
long nextLong1 = scanner.nextLong();
long nextLong2 = scanner.nextLong();
if (nextLong + nextLong1 > nextLong2) {
System.out.println("Case #" + (i + 1) + ": true");
} else {
System.out.println("Case #" + (i + 1) + ": false");
}
}
}
}
第二版:他人代碼(C++)
#include <iostream>
using namespace std;
int main() {
int n;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
long long int a, b, c;
scanf("%lld%lld%lld", &a, &b, &c);
printf("Case #%d: %s\n", i + 1, a + b > c ? "true" : "false");
}
return 0;
}