[A]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).”
Sample Input:
3
1 2 3
2 3 4
9223372036854775807 -9223372036854775808 0
Sample Output:
Case #1: false
Case #2: true
Case #3: false
这道题如果用大整数运算,不,今天不想写了-.-
过几天再来填坑。
先记录另外一种解法(来自算法笔记):
- 两个正数相加溢出结果为 -
- 两个负数相加如果溢出结果为+
但是还有几个小细节需要注意:
long long的范围是[-2^63,2^63-1]
- 如果A B的最大值均为2^63 -1 ,则A + B >= 2^64 - 2,正溢后的区间为 [-2^63,-2] ,-2是由 (2^64 - 2)% 2^64 = -2得到;
- 如果A B的最大值均为-2^63 ,则A + B >= -2^64 ,负溢后的区间为 [0,2^63) ,0是由 (-2^64 )% 2^64 = 0得到;
所以如果数据范围是[-2^63,2^63-1],使用这种方法我觉得会比较合理一点。
最后还有一个问题,如果直接在if语句中判断 a+b是否大于0,就不能完全通过测试点,但是如果先将a+b赋值给某个变量(如res)时,再判断res是否大于0就可以通过。网上查了也没有答案,至今未解。
code:
#include<cstdio>
int main(void){
int n;
long long a,b,c;
scanf("%d",&n);
for(int i = 1; i <= n; i++){
scanf("%lld %lld %lld",&a,&b,&c);
long long res = a + b;
if(a > 0 && b > 0 && res < 0 ) {
printf("Case #%d: true\n",i);
continue;
}
if(a < 0 && b < 0 && res >= 0 ) { //注意这里是 >=
printf("Case #%d: false\n",i);
continue;
}
if( a + b > c)
printf("Case #%d: true\n",i);
else printf("Case #%d: false\n",i);
}
return 0;
}
[A]1065 A+B and C (64bit)(挖坑待填)的更多相关文章
- 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)
1065 A+B and C (64bit) (20 分) Given three integers A, B and C in [−], you are supposed to tell whe ...
- 1065 A+B and C (64bit) (20 分)
1065 A+B and C (64bit) (20 分) Given three integers A, B and C in [−2^63,2^63], you are suppose ...
- 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 ...
- PATA 1065 A+B and C (64bit)
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 分)(大数, Java)
1065 A+B and C (64bit)(20 分) Given three integers A, B and C in [−263,263], you are supposed t ...
- PAT 甲级 1065 A+B and C (64bit) (20 分)(溢出判断)*
1065 A+B and C (64bit) (20 分) Given three integers A, B and C in [−], you are supposed to tell whe ...
- 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) 【大数加法】
题目链接 https://www.patest.cn/contests/pat-a-practise/1065 思路 因为 a 和 b 都是 在 long long 范围内的 但是 a + b 可能会 ...
随机推荐
- Spring读取配置文件 @Value
最近在学习Spring如何读取配置文件,记录下方便自己也方便别人: 大致分为两类吧,一种的思路是利用Spring的beanFactoryPostProcessor读取配置文件内容到内存中,也就是应用程 ...
- es6学习笔记8--Map数据结构
Map Map结构的目的和基本用法 JavaScript的对象(Object),本质上是键值对的集合(Hash结构),但是只能用字符串当作键.这给它的使用带来了很大的限制. var data = {} ...
- UIKit框架之NSObject
首先学习NSObject // // ViewController.m // localization // // Created by City--Online on 15/5/15. // Cop ...
- 使用JDBC一次执行多条语句(以MySQL为例)
阅读本文需要的先修知识: 最基本的SQL语句 最基本的JDBC操作(如插入单条记录) 如急需使用请直接看最后一段代码. 在JDBC中,对记录进行修改操作最简单的方法是使用executeUpdate() ...
- CentOS压力测试 ab 命令安装与使用
Apache安装包中自带的压力测试工具 Apache Benchmark(简称ab) 简单易用,这里就采用 ab作为压力测试工具了. 1.独立安装 ab运行需要依赖apr-util包,安装命令为: y ...
- java设计模式(一)【六大原则】
开发一个系统并不是一件困难的事,但是为何维护好一个系统却是一件让人头疼不以的事? 在笔者的观念中这一切都源自于需求. 如果在软件开发完成之后,需求就不再改变,那大部分程序都不需要维护了.但是, ...
- js-权威指南学习笔记21
第二十一章 多媒体和图形编程 1.为了强制让图片缓存起来,首先利用Image()构造函数来创建一个屏幕外的图片对象,之后将该对象的src属性设置成期望的URL. 2.由于各家浏览器制造商未能在对标准音 ...
- AsyncTask GET请求
布局: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android= ...
- Android 系统中运行jar文件
在android系统中运行jar操作步骤: 1. 打包编译jar包 2. 将jar包导入android设备中 adb push test.jar /data/local/tm ...
- 2018-10-27 22:44:33 c language
2018-10-27 22:44:33 c language 标准的C语言并不支持上面的二进制写法,只是有些编译器自己进行了扩展,才支持二进制数字.并不是所有的编译器都支持二进制数字,只有一部分编译 ...