C++·PAT Grade B 1011. A+B and C (15/15)

/*
1011. A+B and C (15)

Given three integers A, B and C in the interval [-2^31, 2^31], please determine whether A+B is greater than C.

Input format:
    Enter the first line to give a positive integer T (<=10), which is the number of test cases. Then T groups of test cases are given, each group occupies one line, and A, B and C are given in order.
    Integers are separated by spaces.
Output format:
    For each set of test cases, print "Case #X: true" in one line if A+B>C, otherwise output "Case #X: false", where X is the test case number (starting from 1).
Input sample:
4
1 2 3
2 3 4
2147483647 0 2147483646
0 -2147483648 -2147483647
Sample output:
    Case #1: false
    Case #2: true
    Case #3: true
    Case #4: false
*/
/*
    Notice:
        To consider the range of primitive data types
            int [-2^31, 2^31] --> may face the risk of overflow
            double [-10^308, 10^308] --> range safe
*/
#include <iostream>
#include<string.h>
using namespace std;

int main(){
    int size;
    scanf("%d", &size);
    double a,b,c;
    bool *results = new bool[size+1];
    for(int i=1;i<=size;i++){
        cin>>a>>b>>c;
        results[i] = false;
        if(a+b>c){
            results[i] = true;
        }
    }

    for(int i=1;i<=size;i++){
        printf("Case #%d: %s\n", i, results[i]==true?"true":"false");
    }
    return 0;
}

  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324838092&siteId=291194637