PAT甲级.1065. A+B and C (64bit) (20)
来源:互联网 发布:ai软件怎么使用 编辑:程序博客网 时间:2024/06/06 04:51
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.
输入格式:
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.
输出格式:
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).
输入样例:
3
1 2 3
2 3 4
9223372036854775807 -9223372036854775808 0
输出样例:
Case #1: false
Case #2: true
Case #3: false
PAT链接
思路:
考虑到long long格式为[-2^63, 2^63-1]范围,而A+B可能会导致溢出,所以重点是要判断溢出情况。
*1.正溢出(A+B>= 2^63)
A+B <= 2^64-2,正溢出后值得区间为[-2^63, -2] (由(2^64-2)%(2^64) = -2得右边界),故:
if (A>0 && B>0 && res < 0) //正溢出 { flag = true; }
*2.负溢出(A+B<=-2^63-1)
A + B >= (-2^64),故负溢出res值区间[0,2^63-1] (由-2^64 % 2^64 =0 得出左边界)故:
else if (A<0 && B<0 && A + B >= 0) //负溢出 { flag = false; }
*3.没有溢出的时候正常判断即可:
else if (res > C) //else if 不能换为if, 不然可能会覆盖之前的flag { flag = true; } else flag = false;
代码:
/*** @tag PAT_A_1065* @authors R11happy (xushuai100@126.com)* @date 2016-07-20 22:11:16-23:04* @version 1.0* @Language C++* @Ranking 60/5474*/#include <cstdio>#include <cstring>#include <cmath>typedef long long LL;int main(){ int N; LL A, B, C; LL res; //存放结果 bool flag = false; scanf("%d", &N); for (int i = 1; i <= N; i++) { scanf("%lld%lld%lld", &A, &B, &C); res = A + B; if (A>0 && B>0 && res < 0) //正溢出 { flag = true; } else if (A<0 && B<0 && A + B >= 0) //负溢出 { flag = false; } else if (res > C) //else if 不能换为if, 不然可能会覆盖之前的flag { flag = true; } else flag = false; if (flag == true) printf("Case #%d: true\n", i); else printf("Case #%d: false\n", i); } return 0;}
收获:
1.long long 取值为【-2^63, 2^63)左开右闭。
2.超出范围的时候要判断溢出情况,考虑溢出后的取值
3.A+B的值必须用long long 类型 的res存储后才能与C进行比较,不能直接放到if()中进行比较不然测试点2、3过不去。
4.分情况判断时候要用else if 不能一串if,不然可能会出现一种情况满足几个条件,造成后面的flag覆盖前面的(正负溢出应该优先判断)
- PAT甲级.1065. A+B and C (64bit) (20)
- 1065. A+B and C (64bit) (20) PAT 甲级
- 【PAT甲级】1065. A+B and C (64bit) (20)
- PAT甲级1065. A+B and C (64bit) (20)
- 1065. A+B and C (64bit) (20)-PAT甲级
- PAT 甲级 1065. A+B and C (64bit) (20)
- PAT(甲级)1065. A+B and C (64bit)
- 1065. A+B and C (64bit) (20)-PAT甲级真题
- PAT甲级练习1065. A+B and C (64bit) (20)
- PAT甲级真题及训练集(2)--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-A-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)
- IT--硬件--raid
- 2016 Multi-University Training Contest 1 1001 Abandoned country(最小生成树)
- 系列文章之四 用shell结合node.js批量转换excel工具(2)
- 蓝桥杯 ADV-146算法提高 计算器
- js == 与 === 的区别
- PAT甲级.1065. A+B and C (64bit) (20)
- 蓝桥杯 ADV-165算法提高 超级玛丽(动态规划、递推)
- 017——hibernate条件查询
- POJ 3041 Asteroids
- 1.CGI 简介
- 蓝桥杯 ADV-166算法提高 聪明的美食家
- 110. Balanced Binary Tree
- uva 10118 Free Candies dp(分析+状压)
- 实时流分析模式