欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 新闻 > 焦点 > 1065 A+B and C (64bit) (20)

1065 A+B and C (64bit) (20)

2024/11/20 13:36:26 来源:https://blog.csdn.net/2401_88085478/article/details/143896788  浏览:    关键词:1065 A+B and C (64bit) (20)

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

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com