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:
3
1 2 3
2 3 4
9223372036854775807 -9223372036854775808 0
Sample Output:
Case #1: false
Case #2: true
Case #3: false
一开始想到的是大数加法,但是有负数,所以相加很麻烦,参考了网上的答案,利用了long long最大能表示2^63这一特点。
两个[0,2^63)内的数相加,若结果小于零,则是结果太大,溢出,所以一定是大于可以表示的C。
两个(-2^63,0]内的数相加,若结果大约等于零,则是结果太小,溢出,所以一定是小于可以表示的C。
其他情况是没有发生溢出的情况下,按照普通比较方法。
#include "pch.h"
#include <iostream>
#include<string.h>
#include<stdio.h>
#include<iomanip>
#include<vector>
#include<string>
#include<algorithm>
#include<queue>
#include<map>
using namespace std;
int main()
{
int t;
cin >> t;
int num = 1;
for (int i = 0; i < t; i++)
{
long long a, b, c;
cin >> a >> b >> c;
long long res = a + b;
if (a > 0 && b > 0 && res < 0)
cout << "Case #" << num << ": true" << endl;
else if (a < 0 && b < 0 && res>=0)
cout << "Case #" << num << ": false" << endl;
else if (res > c)
cout << "Case #" << num << ": true" << endl;
else
cout << "Case #" << num << ": false" << endl;
num++;
}
}