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

题目来源

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

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.

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).

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,否则为false

分析:

要考虑溢出的问题,我这里用 long double 接收输入

A B C同除1000,然后直接作比较

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实现:

 

 
posted @ 2020-02-05 21:38  47的菠萝~  阅读(128)  评论(0编辑  收藏  举报