PAT 1065 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). 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上不需要这样。

相关推荐
小珑也要变强39 分钟前
队列基础概念
c语言·开发语言·数据结构·物联网
AI原吾3 小时前
掌握Python-uinput:打造你的输入设备控制大师
开发语言·python·apython-uinput
机器视觉知识推荐、就业指导3 小时前
Qt/C++事件过滤器与控件响应重写的使用、场景的不同
开发语言·数据库·c++·qt
毕设木哥3 小时前
25届计算机专业毕设选题推荐-基于python的二手电子设备交易平台【源码+文档+讲解】
开发语言·python·计算机·django·毕业设计·课程设计·毕设
珞瑜·3 小时前
Matlab R2024B软件安装教程
开发语言·matlab
weixin_455446173 小时前
Python学习的主要知识框架
开发语言·python·学习
孤寂大仙v3 小时前
【C++】STL----list常见用法
开发语言·c++·list
她似晚风般温柔7894 小时前
Uniapp + Vue3 + Vite +Uview + Pinia 分商家实现购物车功能(最新附源码保姆级)
开发语言·javascript·uni-app
咩咩大主教4 小时前
C++基于select和epoll的TCP服务器
linux·服务器·c语言·开发语言·c++·tcp/ip·io多路复用
时光飞逝的日子4 小时前
多重指针变量(n重指针变量)实例分析
c语言·指针·多重指针·双重指针·n重指针·指针变量