题目

原题链接

给定区间\[\\(-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

解析

数相加后可能会超过int的表示范围,因此要用long来运算

代码

C++解法

#include <iostream>
using namespace std;

int main() {
    cin.tie(0);
    cin.sync_with_stdio(false);

    int n;
    cin >> n;
    for (int i = 1; i <= n; ++i) {
        long long a, b, c;
        cin >> a >> b >> c;
        cout << "Case #" << i << ": " << (a + b > c ? "true" : "false") << endl;
    }
    return 0;
}

Python解法

n = int(input())
for i in range(n):
    read = [int(i) for i in input().split(" ")]

    print("Case #%d: %s" %
          (i + 1, "true"if read[0] + read[1] > read[2] else"false"))

Java解法

import java.util.*;

class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        for (int i = 1; i <= n; ++i) {
            long a = in.nextLong();
            long b = in.nextLong();
            long c = in.nextLong();

            System.out.printf("Case #%d: %s\n", i, a + b > c ? "true" : "false");
        }
        in.close();
    }
}