1065 A+B and C (64bit) (20 分)
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:
1 | 3 |
Sample Output:
1 | Case #1: false |
作者: HOU, Qiming
单位: 浙江大学
时间限制: 400 ms
内存限制: 64 MB
代码长度限制: 16 KB
题目大意
给出64位表示的a,b,c,要求判断a+b是否>c。
分析
主要就在判断溢出上。a>0且b>0时可能发生溢出,溢出后和必小于0(因为和的最高位为1)。a<0且b<0时也可能溢出,此时和>=0(假设两数最高位为1,其余全0,即LONG_LONG_MIN。此时两数相加,最高位溢出为0,结果为0)。其他情况不会溢出,正常判断即可。
代码
1 |
|
其他
按照我的写法,用clang++可以过,g++则不行,大概是因为编译器的优化。如果将和用long int 存储再比较,那么g++也可以过。