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上不需要这样。

相关推荐
脉动数据行情119 分钟前
Java SpringBoot 对接知名台股 TWSE|批量采集上市股票行情实践
java·开发语言·spring boot
IKUN家族1 小时前
SpringBoot日志
java·开发语言
迷迭香yy1 小时前
Python构建转融券多空识别系统从接口到因子的全流程 IG50免费开源股票数据API接口
开发语言·python·开源
格林威1 小时前
C#图像快速剪切:使用OpenCvSharp和Halcon优化图像剪切和CPU占用
开发语言·人工智能·数码相机·计算机视觉·c#·视觉检测·工业相机
知识分享小能手2 小时前
概理论与数理统计学习教程,从入门到精通,在数理统计中应用R软件(22)
开发语言·学习·r语言
乐观勇敢坚强的老彭2 小时前
C++信奥GESP四级的常见题型和解题模板速查表
开发语言·c++
denggun123452 小时前
信号量(DispatchSemaphore vs AsyncSemaphore)、swift协作式线程池 and python信号量
开发语言·python·swift
lisw052 小时前
计算与科学哲学(Philosophy of Computing and Science)
java·开发语言·人工智能
深念Y3 小时前
为什么选 Go:直观、可维护、AI 友好
linux·开发语言·人工智能·golang·agent·harness
Billy121383 小时前
Day25-编译期计算与 constexpr if:跨越编译与运行的边界
开发语言·c++进阶学习