PAT_A 1065 A+B and C (64bit)
PAT_A 1065 A+B and C (64bit)
分析
本题需要注意的是数据的范围问题,由于使用的是C++,且题目只需要判断大小,使用高精的写法显然没有必要,使用正溢出、负溢出的特性进行判断即可。
在做题的时候还是出现了意想不到的情况,一开始的输入使用的是cin,导致部分测试点(特别是最后一个)无法通过。显示答案错误,后来更换scanf居然通过了,可能long long与cin之间可能还是存在一些奇妙的关系吧...
题目的描述
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
Sample Output:
Case #1: false
Case #2: true
Case #3: false
Thanks to Jiwen Lin for amending the test data.
AC的代码
#include<bits/stdc++.h>
using namespace std;
int main(){
int T,tcase=1;
scanf("%d",&T);
while(T--){
long long a,b,c;
scanf("%lld%lld%lld",&a,&b,&c);
//cin>>a>>b>>c;
long long res=a+b;
bool fl;
if(a>0&&b>0&&res<0)fl=true;
else if (a<0&&b<0&&res>=0)fl=false;
else if (res>c)fl=true;
else fl=false;
if(fl)printf("Case #%d: true\n",tcase++);
else printf("Case #%d: false\n",tcase++);
}
return 0;
}
本文来自博客园,作者:ghosteq,转载请注明原文链接:https://www.cnblogs.com/ghosteq/p/15841226.html