Given three integers A, B and C in [-2^63^, 2^63^], 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
题目大意:判断两数之和是否大于第三个数;
思路:因为输入的数的范围是[-2^63, 2^63] ,且c++中最大整数保存范围不超过2^64,则有相加的时候可能出现溢出;开始想的是把输入的数转换为2进制补码运算,还是比较麻烦,但是是可行的。
;参考了别人的做法;对于溢出的情况不需要算出起正确的值。因为出只有两种情况整数相加,和负数相加。前者之和肯定比第三者大, 后者之后肯定比第三者小。所以当符号相等的时候,只需要判断和是否溢出就能判断三者的大小关系;其余的按照正常的运算来比较
参考地址:https://www.liuchuo.net/archives/2023
#include<iostream> using namespace std; int main(){ long long int a, b, c, sum; int i, n; cin>>n; for(i=0; i<n; i++){ cin>>a>>b>>c; sum=a+b; if(a>0&&b>0&&sum<0) printf("Case #%d: %s ", i+1, "true"); else if(a<0&&b<0&&sum>=0) printf("Case #%d: %s ", i+1, "false"); else printf("Case #%d: %s ", i+1, a+b>c?"true":"false"); } return 0; }