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). Each line should ends with '\n'
.
Sample Input:
3
1 2 3
2 3 4
9223372036854775807 -9223372036854775808 0
Sample Output:
Case #1: false
Case #2: true
Case #3: false
题目大意:给出3个64位以内的整数,问a+b是否大于c。
分析:因为A、B的大小为[-2^63, 2^63],用long long 存储A和B的值,以及他们相加的值sum。如果A、B同号,则可以得到正确的sum;如果A > 0, B > 0,若sum为负数,说明溢出,则一定大于c;
如果A < 0, B < 0,若sum为正数,说明溢出,此时一定小于c。
#include<algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <string>
#include <vector>
#include <cstdio>
#include <queue>
#include <stack>
#include <ctime>
#include <cmath>
#include <map>
#include <set>
#define INF 0xffffffff
#define db1(x) cout<<#x<<"="<<(x)<<endl
#define db2(x,y) cout<<#x<<"="<<(x)<<", "<<#y<<"="<<(y)<<endl
#define db3(x,y,z) cout<<#x<<"="<<(x)<<", "<<#y<<"="<<(y)<<", "<<#z<<"="<<(z)<<endl
#define db4(x,y,z,r) cout<<#x<<"="<<(x)<<", "<<#y<<"="<<(y)<<", "<<#z<<"="<<(z)<<", "<<#r<<"="<<(r)<<endl
using namespace std;int main(void)
{#ifdef testfreopen("in.txt","r",stdin);//freopen("in.txt","w",stdout);clock_t start=clock();#endif //testint n;scanf("%d",&n);for(int i=0;i<n;++i){printf("Case #%d: ",i+1);long long a,b,c,sum=0;scanf("%lld%lld%lld",&a,&b,&c);sum=a+b;if(a>0&&b>0&&sum<0)printf("true\n");else if(a<0&&b<0&&sum>0)printf("false\n");else if(sum>c)printf("true\n");else printf("false\n");}#ifdef testclockid_t end=clock();double endtime=(double)(end-start)/CLOCKS_PER_SEC;printf("\n\n\n\n\n");cout<<"Total time:"<<endtime<<"s"<<endl; //s为单位cout<<"Total time:"<<endtime*1000<<"ms"<<endl; //ms为单位#endif //testreturn 0;
}
或者用python.
T=int(input())
case=1
while T:T=T-1a,b,c=map(int,input().split())if a+b>c:print("Case #{:d}: true".format(case))else:print("Case #{:d}: false".format(case))case=case+1