PAT 1065. A+B and C
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:
3
1 2 3
2 3 4
9223372036854775807 -9223372036854775808 0
Sample Output:
Case #1: false
Case #2: true
Case #3: false
分析
这道题参考了别人的代码
因为A、B的大小为[-2^63, 2^63],用long long 存储A和B的值,以及他们相加的值sum:
如果A > 0, B < 0 或者 A < 0, B > 0,sum是不可能溢出的
如果A > 0, B > 0,sum可能会溢出,sum范围理应为(0, 2^64 – 2],溢出得到的结果应该是[-2^63, -2]是个负数,所以sum < 0时候说明溢出了
如果A < 0, B < 0,sum可能会溢出,同理,sum溢出后结果是大于0的,所以sum > 0 说明溢出了
#include<iostream>
using namespace std;
int main(){
long long a,b,c,sum;
int n;
cin>>n;
for(int i=1;i<=n;i++){
cin>>a>>b>>c;
sum=a+b;
if(a>0&&b>0&&sum<0)
printf("Case #%d: true\n", i);
else if(a<0&&b<0&&sum>=0)
printf("Case #%d: false\n", i);
else if(sum>c)
printf("Case #%d: true\n", i);
else
printf("Case #%d: false\n", i);
}
return 0;
}
PAT 1065. A+B and C的更多相关文章
- PAT 1065 1066 1067 1068
pat 1065 A+B and C 主要是注意一下加法溢出的情况,不要试图使用double,因为它的精度是15~16 ...
- PAT 1065 - 1068 题解
这次的题目来源是 2013 年 10 月 7 日下午的浙大计算机研究生招生机试题. 这次题目的难度,按姥姥的说法是:『比普通的 PAT 要难了 0.5 个点.我是把自己的题目从 1.0 到 5.0 以 ...
- 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 ...
- PAT 1065 单身狗(25)(STL-map+思路+测试点分析)
1065 单身狗(25 分) "单身狗"是中文对于单身人士的一种爱称.本题请你从上万人的大型派对中找出落单的客人,以便给予特殊关爱. 输入格式: 输入第一行给出一个正整数 N(≤ ...
- PAT 1065 A+B and C[大数运算][溢出]
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 分)(大数, Java)
1065 A+B and C (64bit)(20 分) Given three integers A, B and C in [−263,263], you are supposed t ...
- PAT——1065. 单身狗
“单身狗”是中文对于单身人士的一种爱称.本题请你从上万人的大型派对中找出落单的客人,以便给予特殊关爱. 输入格式: 输入第一行给出一个正整数N(<=50000),是已知夫妻/伴侣的对数:随后N行 ...
- PAT 1065 单身狗
https://pintia.cn/problem-sets/994805260223102976/problems/994805266942377984 “单身狗”是中文对于单身人士的一种爱称.本题 ...
- PAT 1065. 单身狗(25)
“单身狗”是中文对于单身人士的一种爱称.本题请你从上万人的大型派对中找出落单的客人,以便给予特殊关爱. 输入格式: 输入第一行给出一个正整数N(<=50000),是已知夫妻/伴侣的对数:随后N行 ...
随机推荐
- 飘逸的python - 极简的二叉树前中后序通杀函数
对于任一结点.能够按某种次序运行三个操作: 訪问结点本身(N) 遍历该结点的左子树(L) 遍历该结点的右子树(R) 用来表示顺序,即,前序NLR/中序LNR/后序LRN. 以下我们用namedtupl ...
- oc49--@class
// Person.h #import <Foundation/Foundation.h> @class Car; //#import "Car.h" // 由于imp ...
- 胜利大逃亡(续)(bfs)
http://acm.hdu.edu.cn/showproblem.php?pid=1429 #include <stdio.h> #include <queue> #incl ...
- go 函数高级运用
一.函数作用域 说明 作用域的定义:已声明标识符所表示的常量.类型.变量.函数或包在源代码中的作用范围 在说函数作用域之前,先简单说一下局部变量和局变量 简单的说: 全局变量:在一个文件中,任何地方都 ...
- Aspnet_Session
cmd: aspnet_regsql.exe -ssadd -sstype c -d ZZCasSession -S 192.168.0.3 -U sa -P szhweb2010 <!--会话 ...
- EditPlus 1:更改默认编码方式
打开软件点击上面的菜单栏Tools(工具),再找到Configure User Tools(用户配置工具)点击,再找到左边栏File点击,这个时候可以看到右边栏的Default encoding点击可 ...
- 自学Python七 爬虫实战一
此文承接上文,让我们写一个简简单单的爬虫,循序而渐进不是吗?此次进行的练习是爬取前5页什么值得买网站中的白菜价包邮信息.包括名称,价格,推荐人,时间. 我们所需要做的工作:1.确定URL并获得页面代码 ...
- Xcode控制台输出中文
创建一个.m文件,然后将一下代码加入.m文件中即可实现控制台输出中文,具体代码如下: #ifndef Release @implementation NSSet(Log) - (NSString *) ...
- 《CSS Mastery》 读书笔记 (1)
--本笔记中英混合,专业名词尽量不翻译,免得误解,如果不习惯,就不用往下看了,走好不送. 第一章 基础 人类的好奇心总是促使我们捣鼓,捣鼓是最好做有效的学习CSS的方法 In this chapter ...
- (转)用JS实现表格中隔行显示不同颜色
用JS实现表格中隔行显示不同颜色 第一种: <style> tr{bgColor:expression( this.bgColor=((this.rowIndex)%2==0 )? ...