PAT 甲级 1065 A+B and C (64bit) (20 分)(溢出判断)*
Given three integers A, B and C in [−], 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 (≤). 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],用long long 存储A和B的值,以及他们相加的值sum:
如果A > 0, B < 0 或者 A < 0, B > 0,sum是不可能溢出的
如果A > 0, B > 0,sum可能会溢出,sum范围理应为(0, 2^64 – 2],溢出得到的结果应该是[-2^63, -2]是个负数,所以sum < 0时候说明溢出了
如果A < 0, B < 0,sum可能会溢出,同理,sum溢出后结果是大于0的,所以sum > 0 说明溢出了
AC代码:
#include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std;
long long a,b,c,sum;
int main(){
int n;
cin>>n;
for(int i=;i<=n;i++){
cin>>a>>b>>c;
sum=a+b;
if(a>&&b>&&sum<=){
cout<<"Case #"<<i<<": "<<"true"<<endl;
}else if(a< &&b< &&sum>=){
cout<<"Case #"<<i<<": "<<"false"<<endl;
}else if(sum>c){
cout<<"Case #"<<i<<": "<<"true"<<endl;
}else{
cout<<"Case #"<<i<<": "<<"false"<<endl;
}
}
return ;
}
PAT 甲级 1065 A+B and C (64bit) (20 分)(溢出判断)*的更多相关文章
- pat 甲级 1065. A+B and C (64bit) (20)
1065. A+B and C (64bit) (20) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 HOU, Qiming G ...
- PAT 甲级 1065. A+B and C (64bit) (20) 【大数加法】
题目链接 https://www.patest.cn/contests/pat-a-practise/1065 思路 因为 a 和 b 都是 在 long long 范围内的 但是 a + b 可能会 ...
- PAT Advanced 1065 A+B and C (64bit) (20 分)(关于g++和clang++修改后能使用)
Given three integers A, B and C in [−], you are supposed to tell whether A+B>C. Input Specificati ...
- PAT甲级:1124 Raffle for Weibo Followers (20分)
PAT甲级:1124 Raffle for Weibo Followers (20分) 题干 John got a full mark on PAT. He was so happy that he ...
- PAT甲级——1065 A+B and C (64bit)
1065 A+B and C (64bit) Given three integers A, B and C in [−263,263], you are supposed to tell ...
- 【PAT甲级】1065 A+B and C (64bit) (20 分)(大数溢出)
题意: 输入三个整数A,B,C(long long范围内),输出是否A+B>C. trick: 测试点2包括溢出的数据,判断一下是否溢出即可. AAAAAccepted code: #defin ...
- PAT A 1065. A+B and C (64bit) (20)
题目 Given three integers A, B and C in [-263, 263], you are supposed to tell whether A+B > C. Inpu ...
- PAT甲题题解-1065. A+B and C (64bit) (20)-大数溢出
第一眼以为是大数据,想套个大数据模板,后来发现不需要.因为A.B.C的大小为[-2^63, 2^63],用long long 存储他们的值和sum. 接下来就是分类讨论:如果A > 0, B & ...
- PAT 甲级 1116. Come on! Let's C (20) 【循环判断】
题目链接 https://www.patest.cn/contests/pat-a-practise/1116 思路 注意一个细节 如果没有本来 ID 的 后来又查了这个ID 不是输出 checked ...
随机推荐
- Vue 项目环境搭建
Vue项目环境搭建 ''' 1) 安装node 官网下载安装包,傻瓜式安装:https://nodejs.org/zh-cn/ 2) 换源安装cnpm >: npm install -g cnp ...
- learning java java.time相关类
var clock = Clock.systemUTC(); System.out.println(clock.instant()); System.out.println(clock.millis( ...
- 洛谷P1039侦探推理题解
#include<cstdio> #include<cstring> #include<string> #include<iostream> using ...
- (2)Angular的开发
什么是 ReactNative应用, 小程序与RN的关系 native端优化 Js端优化 ReactNative是? 用JavaScript来写App,性能好 异步编程 请点赞!因为你的鼓励是我写作的 ...
- GoCN每日新闻(2019-09-24)
1. Go 搭建的高效网页爬虫:https://creekorful.me/building-fast-modern-web-crawler/ 2. Go 时区处理:https://medium.co ...
- 内核中通过进程PID获取进程的全部路径
目录 一丶简介 二丶原理 1.原理 2.代码实现. 一丶简介 我们遇到的Dos路径.如果想转化为NT路径(也就是 C:\xxxx)类似的格式 需要自己实现. 具体原理如下: 二丶原理 1.原理 1.使 ...
- mysql存储html代码之导出后无法导入问题
我用mysql的text类型存储了一些html代码,然后用navicat for mysql导出,然后再次导入的时候,就死活导不进去. mysql提示的错误是:this saves the data ...
- c#调用phantomjs 将 网页 存为 PDF
一. 下载 phantomjs 具体下载方式 不再详细说明了. 二. 创建一个 rasterize.js 文件 (放在哪里都行, 我这里是放在了项目中) . 代码内容如下 var page = req ...
- RHEL7下载
RHEL7下载地址: http://mirrors.aliyun.com/centos/ 我的百度云盘地址: 链接:https://pan.baidu.com/s/1_-bm12bekFlWJiGHj ...
- LDAP服务端 - 调研
一.服务端实现 1.OpenLdap 2.ApacheDS 二.OpenLdap 1.https://segmentfault.com/a/1190000014683418 2.https://www ...