【详细讲解】1065 A+B and C (64bit) (20 分)
立志用最少的代碼做最高效的表達
PAT甲級最優題解——>傳送門
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
1
2
3
4
Sample Output:
Case #1: false
Case #2: true
Case #3: false
數據溢出為環形, 即263+1=2?632^{63}+1 = 2^{-63}263+1=2?63。 同理:2?63?1=2632^{-63}-1= 2^{63}2?63?1=263
因此考慮兩種情況:
溢出:如果a>0, b>0, 但a+b<0,即為溢出。溢出的值肯定比C大。 反之同理。
不溢出:正常判斷即可。
#include<bits/stdc++.h> using namespace std; using gg = long long; int main() {gg n; scanf("%lld", &n); for(gg i = 1; i <= n; i++) {gg a, b, c;scanf("%lld %lld %lld", &a, &b, &c);gg res = a+b, flag;if(a>0 && b>0 && res<0) flag=true; //都溢出了肯定比C大else if(a<0 && b<0 && res>=0) flag=false;//都溢出了肯定比C小else flag = a+b>c;printf("Case #%lld: %s\n", i, (flag ? "true" : "false")); } return 0; }
耗時:
求贊哦~ (?ω?)
總結
以上是生活随笔為你收集整理的【详细讲解】1065 A+B and C (64bit) (20 分)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【最后测试点超时】1063 Set Si
- 下一篇: 【题意+分析】1067 Sort wit