【1065】 A+B and C (64bit) 溢出检测

发布时间:2016-12-10 9:18:38 编辑:www.fx114.net 分享查询网我要评论
本篇文章主要介绍了"【1065】 A+B and C (64bit) 溢出检测",主要涉及到【1065】 A+B and C (64bit) 溢出检测方面的内容,对于【1065】 A+B and C (64bit) 溢出检测感兴趣的同学可以参考一下。

1065. A+B and C (64bit) (20) 时间限制 100 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 HOU, Qiming 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 题意: 给出三个整数, 判断前两个的和是不是大于第三个。 分析: 考察溢出的检测问题, 当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; }