PAT-A 1065. A+B and C (64bit) (20)
2017-02-14 09:09
453 查看
题目链接在此。
首先需要知道一下,这个题目A,B,C的取值范围应该是[-2^63, 2^63)左闭右开,这是经过测试发现后台数据中没有2^63这组数据。
这样一来,我们就可以直接用long long型变量来存储输入的数据。long long 型变量的取值范围是[-2^63,2^63-1]。
在《计算机组成原理》中有两个概念:正溢出和负溢出。
有了这个结论,那么我们就可以直接根据不同的情况,来进行解题了,直接看代码即可。
注意点:
1. 判断负溢出的时候需要判断res>=0,而不是>0,否则最后一个测试点过不去。
2. a+b的结果必须放到long long型变量中才能与c进行比较,不能直接用a+b>c这种形式,而应该是res = a+b; res>c这种形式,否则后两个测试点过不去。这里有人讨论过这个问题。
题意理解
一拿到这个题,我想到的是大数运算,不过大数运算绝对可以解决这个问题。后来看《算法笔记》知道了现在这种解法。首先需要知道一下,这个题目A,B,C的取值范围应该是[-2^63, 2^63)左闭右开,这是经过测试发现后台数据中没有2^63这组数据。
这样一来,我们就可以直接用long long型变量来存储输入的数据。long long 型变量的取值范围是[-2^63,2^63-1]。
在《计算机组成原理》中有两个概念:正溢出和负溢出。
正溢出:两个整数相加的结果超过了数据类型所能表示的最大范围,结果为负数 负溢出:两个负数相加的结果超过了数据类型所能表示的最小范围,结果为正数(包括0)
有了这个结论,那么我们就可以直接根据不同的情况,来进行解题了,直接看代码即可。
AC代码
#include<stdio.h> #include<string.h> int main(){ int n; scanf("%d",&n); long long a,b,c; long long res; bool flag; for(int i = 0; i < n; i++){ flag = false; scanf("%lld %lld %lld",&a,&b,&c); res = a+b; if(a>0 && b>0){ if(res < 0){ //正溢出 flag = true; }else{ if(res > c){ flag = true; } } }else if(a<0 && b<0){ if(res >= 0){ //负溢出 flag = false; }else{ if(res > c){ flag = true; } } }else{ if(res > c){ flag = true; } } if(flag == true){ printf("Case #%d: true\n",i+1); }else{ printf("Case #%d: false\n",i+1); } } return 0; }
注意点:
1. 判断负溢出的时候需要判断res>=0,而不是>0,否则最后一个测试点过不去。
2. a+b的结果必须放到long long型变量中才能与c进行比较,不能直接用a+b>c这种形式,而应该是res = a+b; res>c这种形式,否则后两个测试点过不去。这里有人讨论过这个问题。
相关文章推荐
- PAT (Advanced Level) 1065. A+B and C (64bit) (20)
- PAT甲级真题及训练集(2)--1065. A+B and C (64bit) (20)
- PAT 甲级 1065. A+B and C (64bit) (20) 【大数加法】
- PAT-A-1065. A+B and C (64bit) (20)
- PAT-A-1065. A+B and C (64bit) (20)
- PAT - 甲级 - 1065. A+B and C (64bit) (20)(模拟)
- PAT (Advanced Level) 1065. A+B and C (64bit) (20) 溢出判断
- 浙江大学PAT_甲级_1065. A+B and C (64bit) (20)
- PAT 1065. A+B and C (64bit) (20) 大整数,有正负,加减。疑问已解决
- PAT 1065. A+B and C (64bit) (20)
- PAT 1065. A+B and C (64bit) (20)
- ZJU-PAT 1065. A+B and C (64bit) (20)
- 【PAT】【Advanced Level】1065. A+B and C (64bit) (20)
- PAT 1065. A+B and C (64bit) (20)
- pat 甲级 1065. A+B and C (64bit) (20)
- 【PAT】1065. A+B and C (64bit) (20)
- 【C++】PAT(advanced level)1065. A+B and C (64bit) (20)
- 浙大PAT 1065. A+B and C (64bit) (20)
- PAT 1065. A+B and C (64bit) (20)
- ZJU-PAT 1065. A+B and C (64bit) (20)