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 0Sample Output:
Case #1: false Case #2: true Case #3: false
给出三个整数, 判断前两个的和是不是大于第三个。
分析:
考察溢出的检测问题, 当A和B同时为正整数或者同时为负整数的时候要小心题目的和可能溢出, 所以不能简单的return A+B > C, 要加溢出的判断代码。
#define LLONG_MAX 9223372036854775807i64 /* maximum signed long long int value */
#define LLONG_MIN (-9223372036854775807i64 - 1) /* minimum signed long long int value */
代码:
#include <iostream> #include <fstream> #include <climits> using namespace std; ifstream fin("in.txt"); #define cin fin bool Compare(const long long& a,const long long& b,const long long& c){ if(a>0 && b>0){ if(a > LLONG_MAX - b)return true; } if(a<0 && b<0){ if(a < LLONG_MIN -b)return false; } return a+b > c; } int main() { int n; cin>>n; long long a,b,c; int i; for(i=0;i<n;i++) { cin>>a>>b>>c; cout<<"Case #"<<i+1<<": "; if(Compare(a,b,c)){ cout<<"true"<<endl; }else cout<<"false"<<endl; } system("PAUSE"); return 0; }