[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 可能会 ...
随机推荐
- ActiveMQ专题2: 持久化
AMQ的持久化问题 前言 前面一篇AMQ专题中,我们发现对于Topic这种类型的消息,即使将deliveryMode设置为持久化,只要生产者在消费者之前启动.消息生产者发布的消息还是会丢失.这是符 ...
- Java SDK夯住(Hang)问题排查
夯住(Hang)是指程序仍在运行,卡在某个方法调用上,没有返回也没有异常抛出:卡住时间从几秒到几小时不等. Java程序发生Hang时,应该首先使用 jstack 把java进程的堆栈信息保存下来 , ...
- Docker swarm 实战-部署wordpress
Docker swarm 实战-部署wordpress 创建一个overlay的网络 docker network create -d overlay demo 6imq8da3vcwvj2n499k ...
- 定时器--Quartz.Net
这次由于项目的需求:什么定时发送邮件通知,定时筛选取消客户下单未支付的订单 重新捡起定时器,在网上翻来找去找到----Quartz.Net老字号了并不表示它就真的老了哦 github:https:// ...
- Java基础——关于接口和抽象类的几道练习题
呃,一定要理解之后自己敲!!!这几道题,使我进一步了解了接口和抽象类. 1.设计一个商品类 字段: 商品名称,重量,价格,配件数量,配件制造厂商(是数组,因为可能有多个制造厂商) 要求: 有构造函数 ...
- windows 下 MyEclipse 逆向工程生成hiberate 对应配置文件以及 javaBean。
步骤1: 右边工具栏 -> 右击你的项目 -> 选中 MyEclipse -> Project Facets -> install Hibernate Facet -> ...
- 使用commons-pool2实现FTP连接池
GitHub : https://github.com/jayknoxqu/ftp-pool 一. 连接池概述 频繁的建立和关闭连接,会极大的降低系统的性能,而连接池会在初始化的时候会创建一定 ...
- java中递归实现复制多级文件夹
常见的流的用法 递归实现复制多级文件夹 FileInputStream & FileOutputStream String content = null;//用来储存解码后的byte数组 in ...
- 理解Java异常
一.Java异常的简介 Java异常是Java提供的一种识别及响应错误的一致性机制.具体来说,异常机制提供了程序退出的安全通道.当出现错误后,程序执行的流程发生改变,程序的控制权转移到异常处理器.Ja ...
- Chromium的Grit工具解析
转载请注明出处:http://www.cnblogs.com/fangkm/p/3405959.html Chromium项目采用Grit工具来打包生成程序需要的资源,如图片资源.字符串资源等,尤其是 ...