栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

【PAT甲级】简单模拟题错题整理(持续更新)

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【PAT甲级】简单模拟题错题整理(持续更新)

A1065. A+B and C (64bit)

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

该题没有涉及数据结构和算法,只是简单模拟,做题过程中没有考虑到溢出问题,A和B的范围都在[−2^ 63,2^63],所以A+B会有可能超出long long的范围,所以一定要考虑到溢出。

若A>0,B>0时,A+B<0,说明正溢出,A+B一定大于C,为true;
若A<0,B<0时,A+B>0,说明负溢出,A+B一定小于C,为false;

代码:

#include 
 
int main(){
 
	int t;
	
	scanf("%d",&t);
	
	for(int i =1;i<=t;i++){
		long long a,b,c,ans;
		scanf("%lld%lld%lld",&a,&b,&c);
		
		ans = a+b;
		if(a>0&&b>0&&ans<0){
		printf("Case #%d: truen",i);
		}
		else if(a<0&&b<0&&ans>=0){
		printf("Case #%d: falsen",i);
		}
 
		else if(ans>c)	printf("Case #%d: truen",i);
		else printf("Case #%d: falsen",i);
 
	}
	return 0;
} 
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/928516.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号