1065. A+B and C (64bit) (20) 大数据溢出问题

来源:互联网 发布:js多图上传前本地预览 编辑:程序博客网 时间:2024/05/24 06:00


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,b,c三个数字,要求判断a+b的值和c相比是大还是小,如果是大,输出true,否则输出false


分析:

题目给定的数的范围会出现越界,a,b,c都要使用long long型,sum = a+b

当判断越界的时候:

1.当a>0,b>0,sum<0,此时就是sum越界,但是c始终在long long型的范围中,所以此时sum肯定是大于c的,输出true

2.当a<0,b<0,sum>0,此时也是sum越界,说明sum足够小,使得sum越过最小界,但是c始终在范围之中,所以c大于sum,输出false

3.除了这两种情况,剩下来的就是不越界的情况,只需要判断sum和c的大小就可以


#include <iostream>#include <cstdio>#include <algorithm>#include <cstring>#include <vector>#include <set>#include <map>using namespace std;const int maxn = 1e5+10;int main(){    int n;    cin >> n;    long long sum,a,b,c;    int cnt = 1;    while(n--)    {       cin >> a >> b >> c;       sum = a+b;       if(a > 0&&b > 0&&sum < 0)       printf("Case #%d: true\n",cnt++);       else if(a < 0&&b < 0&&sum >= 0)       printf("Case #%d: false\n",cnt++);       else if(sum > c)       printf("Case #%d: true\n",cnt++);       else        printf("Case #%d: false\n",cnt++);    }return 0;}