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

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

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

相关推荐
XH华14 分钟前
C++语言第二章类和对象(下)
开发语言·c++
从零开始的代码生活_24 分钟前
C++ stack、queue 与 priority_queue:容器适配器原理与实战
开发语言·c++·后端·学习·算法
techdashen1 小时前
Go 1.25 新增 `reflect.TypeAssert`:更直接、更高效地从 `reflect.Value` 取出类型值
开发语言·后端·golang
2zcode1 小时前
项目文档:基于MATLAB低采样率ISAR成像的快速稀疏重建算法研究
开发语言·算法·matlab
xixi09241 小时前
JMeter5.6.3基础使用教程1
java·开发语言
এ慕ོ冬℘゜1 小时前
原生 JS 实现自定义单选日历组件(无 UI 库、纯手写、禁用过去日期)
开发语言·javascript·ui
Ulyanov1 小时前
刚体动力学方程——牛顿-欧拉与陀螺效应的奥秘
开发语言·python·目标跟踪·雷达电子对抗·导引头
罗超驿1 小时前
15.Java异常处理:从束手无策到从容应对
java·开发语言
MoonBit月兔2 小时前
MoonBit v0.10.4版本更新
开发语言·人工智能·编程·moonbit
小樱花的樱花2 小时前
Linux 多线程编程:互斥锁(Mutex)详解
linux·c语言·开发语言