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

题目描述

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

翻译:给你桑整数A,B,C 范围在[-2^63, 2^63]之间,你需要输出A+B是否大于C。

INPUT FORMAT

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.

翻译:输入第一行为测试数据的组数T(<=10)。接着T组测试数据,每组包括一行三个整数A,B,C,之间用空格隔开。

OUTPUT FORMAT

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

翻译:对于每组输入数据,输出一行”Case #X: true”如果A+B>C,否则输出”Case #X: false” ,X为测试数据的次数。


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要先保存到一个long long int类型变量中,否则数据仍按照int进行计算。数据需要判断溢出,即A>0&&B>0但是C<0,这种情况一定是true;A<0&&B<0但是A+B>=0,这种情况一定是false,注意负负相加可以为0。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<algorithm>
#define INF 99999999
using namespace std;
long long int A,B,C; 
int main(){
    int T;
    scanf("%d",&T); 
    for(int i=1;i<=T;i++){
        scanf("%lld%lld%lld",&A,&B,&C);
        long long int sum=A+B;
        printf("Case #%d: ",i);
        if(A<0&&B<0&&sum>=0)printf("false\n");
        else if(A>0&&B>0&&sum<0)printf("true\n");
        else if(sum<=C)printf("false\n");
        else if(sum>C)printf("true\n");
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值