[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 可能会 ...
随机推荐
- Netbeans 8.0配置Python开发环境
1. 菜单栏:工具->插件->设置->添加 配置如下信息: http://deadlock.netbeans.org/hudson/job/nbms-and-javadoc/last ...
- AutoMapper之嵌套映射
8.嵌套映射 嵌套映射就是一个类中包含有另一个类,这种情况下我们应该如何映射呢? /// <summary> /// 源对象 /// </summary> public cla ...
- JS DOM操作(五) Window.docunment对象——操作元素
定位: var a = document.getElementByIt( "id" ) 同辈元素 var b = a.nextSibling; -- 找 a ...
- [日常] Go语言圣经--Channel习题
练习 8.3: 在netcat3例子中,conn虽然是一个interface类型的值,但是其底层真实类型是*net.TCPConn,代表一个TCP连接.一个TCP连接有读和写两个部分,可以使用Clos ...
- EF DataFirst修改数据类型
在做软件的时候我们可能会遇到这样的问题,就是在使用EF的时候,有时候精度不一样, 我们用整数来计算肯定是比浮点数来得快的,但我在MySQL里面存储的数据类型是decimal的,我生成EF后, 里面的数 ...
- python移位运算符
1,二进制方式 >>> bin( 1 ) '0b1' >>> bin( 10 ) '0b1010' >>> a = 0b10 >>&g ...
- MySQL常用操作汇编
熟悉 我熟悉xxx,其实很多原来熟悉到能背的,如果长时间不用了几乎也就忘了.此时再说自己熟悉XXX就被认为是在吹牛B了,感觉不是很好.所谓温故而知新,对于天资不聪颖的,就是要在一遍一遍的复习实践中慢慢 ...
- python 判断字符串是字母 数字 大小写还是空格
str.isalnum() 所有字符都是数字或者字母,为真返回 Ture,否则返回 False. str.isalpha() 所有字符都是字母(当字符串为中文时, 也返回True),为真返回 T ...
- webstorm技巧
webstorm安装后的一些设置技巧: 如何更改主题(字体&配色):File -> settings -> Editor -> colors&fonts -> ...
- 设定linux为多用户模式
1.显示系统已经安装的组件,和可以安装的组件: #yum grouplist 2.如果系统安装之初采用最小化安装,没有安装xwindow,那么先安装: #yum groupinstall " ...