PAT 1065 a+b>c 64bit 翻译 分析 代码

本文介绍了一种方法来判断三个64位整数A、B和C中,A+B是否大于C,并考虑了加法操作可能引起的溢出问题。使用C++实现,通过检查加法结果的符号来间接判断是否发生了溢出。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目:1065 a+b>c 64bit (20)(20 分)【加值溢出】

Given three integers A, B and C in [-2^63^, 2^63^], you are supposed to tell whether A+B > C.

【给出三个整数A,B,C,三者均有限制,[-2^63^, 2^63],判断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.

【第一行给出整数T,表明T组数据。每行数据都包括A,B,C 期间被空格隔开】

Output Specification:

For each test case, output in one line "Case #X: true" if A+B&gtC, or "Case #X: false" otherwise, where X is the case number (starting from 1).

【如果 A+B > C ,输出Case #X: true;如果A+B < C ,输出Case #X: false 

【注】:以下分析所在为代码注释,通过注释可以分析得到此题答案。

【注】:此代码是由Dev c++ 5.11 所写,运用c++语言。  其他方法后续会另写博客给出方式。惊讶

#include<cstdio>
int main()
{

	int i,K;
	scanf("%d",&K);
	long long  a,b,c;  //长整数类型 
	bool jud;
	for(i=1;i<=K;i++)
	{
		scanf("%lld%lld%lld",&a,&b,&c);
		long long sum=a+b;			//二者加和值也必为长整数类型 
		if(a>0&&b>0&&sum<0) 		jud=true;		//a,b均为正数,且加值发生溢出,若溢出值为负数,则a+b>c; 
		else if(a<0&&b<0&&sum>=0)	jud=false;		//a,b均为负数,且加值发生溢出,若溢出值为正数,则a+b<c;
		else if(sum>c) 				jud=true;		//如果加值大于C,则说明没有溢出,且a+b>c; 
		else 						jud=false;		//其他情况,则为a+b<c; 
		if(jud==true)	printf("Case #%d: true\n", i);       //如果判断值为正,输出相应反应; 
		if(jud==false)	printf("Case #%d: false\n", i);      //如果判断值为负,输出相应反应; 
	}
	return 0;
 } //唱晚,我爱你  2018年7月2日17:29:57 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值