两数相加出现正溢出和负溢出
1. 原文
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 |
2. 解析
给出三个整数A,B,C,判断A+B>C
由于long long范围为 [−263,263],计算机组成中指出,两个正数相加为负数,两个负数相加为正数,则为溢出。
A,B最大为 263-1,两数相加为264-2,超过long long正向最大值而出现正溢出,正溢出区间[-263,-2]<0,
(264-2)%264 = -2
return true
A,B最小为 -263,两数相加为-264,超过long long负向最小值而出现负溢出,负溢出区间[0,-263)>=0,
-264%264 = 0
return false
3. AC代码
1 |
|