1065. A+B and C (64bit) (20) 大数据溢出问题
来源:互联网 发布:js多图上传前本地预览 编辑:程序博客网 时间:2024/05/24 06:00
Given three integers A, B and C in [-263, 263], 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:31 2 32 3 49223372036854775807 -9223372036854775808 0Sample Output:
Case #1: falseCase #2: trueCase #3: false
题意:
给定a,b,c三个数字,要求判断a+b的值和c相比是大还是小,如果是大,输出true,否则输出false
分析:
题目给定的数的范围会出现越界,a,b,c都要使用long long型,sum = a+b
当判断越界的时候:
1.当a>0,b>0,sum<0,此时就是sum越界,但是c始终在long long型的范围中,所以此时sum肯定是大于c的,输出true
2.当a<0,b<0,sum>0,此时也是sum越界,说明sum足够小,使得sum越过最小界,但是c始终在范围之中,所以c大于sum,输出false
3.除了这两种情况,剩下来的就是不越界的情况,只需要判断sum和c的大小就可以
#include <iostream>#include <cstdio>#include <algorithm>#include <cstring>#include <vector>#include <set>#include <map>using namespace std;const int maxn = 1e5+10;int main(){ int n; cin >> n; long long sum,a,b,c; int cnt = 1; while(n--) { cin >> a >> b >> c; sum = a+b; if(a > 0&&b > 0&&sum < 0) printf("Case #%d: true\n",cnt++); else if(a < 0&&b < 0&&sum >= 0) printf("Case #%d: false\n",cnt++); else if(sum > c) printf("Case #%d: true\n",cnt++); else printf("Case #%d: false\n",cnt++); }return 0;}
- 1065. A+B and C (64bit) (20) 大数据溢出问题
- PAT (Advanced Level) 1065. A+B and C (64bit) (20) 溢出判断
- 【1065】 A+B and C (64bit) 溢出检测
- 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)
- 1065. A+B and C (64bit)
- 1065. A+B and C (64bit)
- 1065. A+B and C (64bit)
- 1065.A+B and C (64bit)
- 1065. A+B and C (64bit)
- 1065. A+B and C (64bit)
- 1065. A+B and C (64bit)
- 1065. A+B and C (64bit)
- 1065. A+B and C (64bit)
- 1065. A+B and C (64bit)
- A+B and C (64bit) (20)
- 1065. A+B and C (64bit) (20)
- 一份react-native面试题
- Ubuntu17.04双系统的安装笔记(win7下)
- 先学会这些java所谓的基础
- PHP TP3.2 验证码的示例
- 全志H3-NanoPi开发板SDK之一总体概述
- 1065. A+B and C (64bit) (20) 大数据溢出问题
- TIME_WAIT和CLOSE_WAIT
- zcmu-1201
- 字符串操作助手类
- 微服务与架构入门
- 哈哈日语入门六项规定要严格遵守
- 洛谷Oj-吃奶酪-深度优先搜索+剪枝
- 数学建模专栏 | 第二篇 :MATLAB 数学建模快速入门
- eclipse中找不到base64包的解决方法