PAT 甲級 1065.A+B and C C++/Java

題目來源html

Given three integers A, B and C in [−], you are supposed to tell whether A+B>C.ios

Input Specification:

The first line of the input gives the positive number of test cases, T (≤). Then T test cases follow, each consists of a single line containing three integers A, B and C, separated by single spaces.spa

Output Specification:

For each test case, output in one line Case #X: true if A+B>C, or Case #X: false otherwise, where X is the case number (starting from 1).code

Sample Input:

3
1 2 3
2 3 4
9223372036854775807 -9223372036854775808 0
 

Sample Output:

Case #1: false
Case #2: true
Case #3: false

題目大意:

給定三個數A B C,A + B > C爲true,不然爲falsehtm

分析:

要考慮溢出的問題,我這裏用 long double 接收輸入blog

A B C同除1000,而後直接做比較three

C++實現:

#include <iostream>
using namespace std;
int main() {
    long double A, B, C;
    int N;
    cin >> N;
    for (int i = 1; i <= N; ++i) {
        cin >> A >> B >> C;
        A /= 1000;
        B /= 1000;
        C /= 1000;
        cout << "Case #" << i << ": ";
        if (A + B > C) {
            cout << "true" << endl;
        }
        else {
            cout << "false" << endl;
        }
    }
    return 0;
}

 

 

 

Java實現:

相關文章
相關標籤/搜索