1065. A+B and C (64bit) (20)

来源:互联网 发布:永宏编程手册 编辑:程序博客网 时间:2024/06/03 13:54

Given three integers A, B and C in [-263, 263], 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 (<=10). 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:
31 2 32 3 49223372036854775807 -9223372036854775808 0
Sample Output:
Case #1: falseCase #2: trueCase #3: false

此题目的所有数据都在整型范围内,但是如果两数相加后需要进位,就会超出范围。所以此题的关键在于,通过判断每个数的正负,规避加操作
分为 第一种:a>=0且b>=0
第二种 a>=0&&b<0 或者 a< 0 &&b>=0
第三种 a<0&&b<0
之后每种又分c>=0和c<0两种情况。

#include<iostream>#include<cstdio>using namespace std;int main(){    int n;    long long a,b,c;    cin>>n;    for (int i=0;i<n;i++)    {        bool flag =true;        scanf("%lld %lld %lld",&a,&b,&c);        if (a>=0&&b>=0)        {            if (c>=0)                flag=a>c-b;            else                flag=true;        }        else if ((a>=0&&b<0)||(b>=0&&a<0))        {                flag = a+b>c;        }        else if (a<0&&b<0)        {            if (c>=0)                flag=false;            else                flag=a>c-b;        }        printf("Case #%d: %s\n",i+1,flag?"true":"false");    }}
原创粉丝点击