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的更多相关文章

  1. PAT 1065 1066 1067 1068

    pat 1065 A+B and C                                          主要是注意一下加法溢出的情况,不要试图使用double,因为它的精度是15~16 ...

  2. PAT 1065 - 1068 题解

    这次的题目来源是 2013 年 10 月 7 日下午的浙大计算机研究生招生机试题. 这次题目的难度,按姥姥的说法是:『比普通的 PAT 要难了 0.5 个点.我是把自己的题目从 1.0 到 5.0 以 ...

  3. 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 ...

  4. 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 ...

  5. PAT 1065 单身狗(25)(STL-map+思路+测试点分析)

    1065 单身狗(25 分) "单身狗"是中文对于单身人士的一种爱称.本题请你从上万人的大型派对中找出落单的客人,以便给予特殊关爱. 输入格式: 输入第一行给出一个正整数 N(≤  ...

  6. PAT 1065 A+B and C[大数运算][溢出]

    1065 A+B and C (64bit)(20 分) Given three integers A, B and C in [−2​63​​,2​63​​], you are supposed t ...

  7. pat 1065 A+B and C (64bit)(20 分)(大数, Java)

    1065 A+B and C (64bit)(20 分) Given three integers A, B and C in [−2​63​​,2​63​​], you are supposed t ...

  8. PAT——1065. 单身狗

    “单身狗”是中文对于单身人士的一种爱称.本题请你从上万人的大型派对中找出落单的客人,以便给予特殊关爱. 输入格式: 输入第一行给出一个正整数N(<=50000),是已知夫妻/伴侣的对数:随后N行 ...

  9. PAT 1065 单身狗

    https://pintia.cn/problem-sets/994805260223102976/problems/994805266942377984 “单身狗”是中文对于单身人士的一种爱称.本题 ...

  10. PAT 1065. 单身狗(25)

    “单身狗”是中文对于单身人士的一种爱称.本题请你从上万人的大型派对中找出落单的客人,以便给予特殊关爱. 输入格式: 输入第一行给出一个正整数N(<=50000),是已知夫妻/伴侣的对数:随后N行 ...

随机推荐

  1. THRDTERM-----干净地结束一个线程

    THRDTERM产生两个线程.周期性地检查一个event对象.以决定要不要结束自己. #define WIN32_LEAN_AND_MEAN #include<stdio.h> #incl ...

  2. sharepoint类型转换

    sharepoint学习汇总 http://blog.csdn.net/qq873113580/article/details/20390149 r[col.ColumnName] = GetType ...

  3. luogu1169 棋盘制作

    题目大意 有一个有m*n个格子的矩形,每个格子都有黑或白两种颜色.现要求将该矩形分别裁剪成一个小矩形或一个小正方形,使得这个矩形和正方形是个国际象棋棋盘,且面积最大. 题解 首先,为了简化问题,我们每 ...

  4. C# winform KeyPress 事件中对应的数字

    C#  winform KeyPress 事件中对应的数字所有e.KeyChar值的意思 常用ASCII码表 你自己看看应该就明白了 键盘 ASCII码 键盘 ASCII码 ESC 27 7 55 S ...

  5. 循环遍历Java字符串字符的规范方法——类似python for ch in string

    比如我将string作为CNN 文本处理输入: float [] input = new float[maxLength]; // 1 sentence by maxLenWords // int[] ...

  6. csharp OverflowException——超出数值范围会抛出异常

    OverflowException 會在下列情況下執行階段擲回︰ 算術運算會產生作業所傳回的資料型別範圍之外的結果. 下列範例說明 OverflowException 超出範圍的乘法運算所擲回 Int ...

  7. 【HDU 3663】 Power Stations

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=3663 [算法] 先建图,然后用Dancing Links求解精确覆盖,即可 [代码] #inclu ...

  8. [SpringMVC]定义多个前缀映射的问题

    转自:https://penciltim.iteye.com/blog/501073 我在web.xml里面定义多个dispatch-servlet的前缀映射,像下面这样 <!-- Servle ...

  9. Android对话框与Activity共存时的异常

    异常提示信息 01-01 18:30:38.630: E/WindowManager(14537): Activity com.jack.outstock.activity.ManageCustomA ...

  10. PCB Genesis原点坐标转换关系

    一.Genesis原点坐标转换关系: 1.读取Genesis坐标转换:   UI界面坐标 = 文件坐标 - 偏移值 2.写入Genesis坐标转换:   文件坐标 = UI界面坐标 + 偏移值 3.为 ...