PAT 1065 A+B and C (64bit)

个人学习记录,代码难免不尽人意。

Given three integers A, B and C in (−263,263), 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

cpp 复制代码
#include<cstdio>
#include<iostream>
typedef long long LL;
using namespace std;
int main(){
	int n;
	scanf("%d",&n);
	bool ans[n]={false};
	for(int i=0;i<n;i++){
		LL a;
		LL b;
		LL c;
		LL res;
		cin >> a >> b >> c;
		int ap=a%10;
		int bp=b%10;
		int cp=c%10;
		res=(a/10)*10+(b/10)*10+ap+bp;
		if(res>(c/10)*10+cp) ans[i]=true;
		
		
	} 
	for(int i=0;i<n;i++){
		if(ans[i])printf("Case #%d: true\n",i+1);
		else printf("Case #%d: false\n",i+1);;
	}
}

因为longlong类型的存储范围为[-263,263),所以题目中部分数据可能会超出限制,因此我们需要处理这部分数据,我的想法是这样的,将原数据范围处理到[-262,262],然后再加上A/B/C末位数来判断。举个例子,比如A=13,B=5,C=23,相当于判断 10+3+5是否大于20+3,这样就避免了数据越界了。

而在《算法笔记》上的解法是根据计算机组成原理来解的,如果当两个正数相加等于负数或者两个负数相加等于正数的话就是越界。这样做也可以。

PS:我设置bool数组是当时觉得需要一次性输出所有结果,但现在看来在PTA上不需要这样。

相关推荐
独行soc几秒前
2025年渗透测试面试题总结-百度面经(题目+回答)
运维·开发语言·经验分享·学习·面试·渗透测试·php
免檒8 分钟前
go语言协程调度器 GPM 模型
开发语言·后端·golang
海棠蚀omo10 分钟前
C++笔记-红黑树
开发语言·c++·笔记
雪芽蓝域zzs34 分钟前
JavaScript splice() 方法
开发语言·javascript·ecmascript
休息一下接着来43 分钟前
C++ I/O多路复用
linux·开发语言·c++
jz_ddk2 小时前
[学习] RTKLib详解:rtcm2.c、rtcm3.c、rtcm3e与rtcmn.c
c语言·学习·算法
代码狂人2 小时前
Lua中使用module时踩过的坑
开发语言·lua
繁依Fanyi2 小时前
ColorAid —— 一个面向设计师的色盲模拟工具开发记
开发语言·前端·vue.js·编辑器·codebuddy首席试玩官
darkchink2 小时前
[LevelDB]LevelDB版本管理的黑魔法-为什么能在不锁表的情况下管理数据?
c语言·数据库·c++·oracle·数据库开发·dba·db
易只轻松熊2 小时前
C++(23):容器类<vector>
开发语言·数据结构·c++