Given three integers A, B and C in [-2^63, 2^63], 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


这道题如果用大整数运算,不,今天不想写了-.-
过几天再来填坑。
先记录另外一种解法(来自算法笔记):

  • 两个正数相加溢出结果为 -
  • 两个负数相加如果溢出结果为+

但是还有几个小细节需要注意:
long long的范围是[-2^63,2^63-1]

  • 如果A B的最大值均为2^63 -1 ,则A + B >= 2^64 - 2,正溢后的区间为 [-2^63,-2] ,-2是由 (2^64 - 2)% 2^64 = -2得到;
  • 如果A B的最大值均为-2^63 ,则A + B >= -2^64 ,负溢后的区间为 [0,2^63) ,0是由 (-2^64 )% 2^64 = 0得到;
    所以如果数据范围是[-2^63,2^63-1],使用这种方法我觉得会比较合理一点。

最后还有一个问题,如果直接在if语句中判断 a+b是否大于0,就不能完全通过测试点,但是如果先将a+b赋值给某个变量(如res)时,再判断res是否大于0就可以通过。网上查了也没有答案,至今未解。

code:
#include<cstdio>

int main(void){
    int n;
    long long a,b,c;
    scanf("%d",&n);
    for(int i = 1; i <= n; i++){
        scanf("%lld %lld %lld",&a,&b,&c);

        long long res = a + b;

        if(a > 0 && b > 0 && res < 0 ) {
            printf("Case #%d: true\n",i);
            continue;
        }
        if(a < 0 && b < 0 && res >= 0 ) { //注意这里是 >=
            printf("Case #%d: false\n",i);
            continue;
        }

        if( a + b > c)
            printf("Case #%d: true\n",i);
        else    printf("Case #%d: false\n",i);
    }

    return 0;
}

[A]1065 A+B and C (64bit)(挖坑待填)的更多相关文章

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

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

  3. 1065 A+B and C (64bit) (20 分)

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

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

  5. PATA 1065 A+B and C (64bit)

    1065. A+B and C (64bit) (20) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 HOU, Qiming G ...

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

  7. PAT 甲级 1065 A+B and C (64bit) (20 分)(溢出判断)*

    1065 A+B and C (64bit) (20 分)   Given three integers A, B and C in [−], you are supposed to tell whe ...

  8. PAT甲级——1065 A+B and C (64bit)

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

  9. PAT 甲级 1065. A+B and C (64bit) (20) 【大数加法】

    题目链接 https://www.patest.cn/contests/pat-a-practise/1065 思路 因为 a 和 b 都是 在 long long 范围内的 但是 a + b 可能会 ...

随机推荐

  1. LVS持久化与超时时间问题分析

    前言 在上一篇文章<搭建DNS+LVS(keepAlived)+OpenResty服务器(Docker环境)>中,我搭建了dns+lvs+openresty+web集群:先来回顾一下架构图 ...

  2. 设置tabBar、使用第三方插件和自定义组件使用简单实例

    创建小程序项目进入时填写,因需要用上第三方插件,所以要填上开发者的APPID,前往微信公众平台去注册一个账号获取APPID,在设置=>开发设置可以查看相关appid信息 简单思路 底部导航添加三 ...

  3. Windows程序

    Windows程序是事件驱动的,对于一个窗口,它的大部分例行维护是由系统维护的.每个窗口都有一个消息处理函数.在消息处理函数中,对传入的消息进行处理.系统内还有它自己的缺省消息处理函数.     客户 ...

  4. JS去掉字符串前后空格或去掉所有空格的用法

    1.  去掉字符串前后所有空格: 代码如下: function Trim(str) { return str.replace(/(^\s*)|(\s*$)/g, ""); } 说明 ...

  5. Graveyard(poj3154)

    Graveyard Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 1289   Accepted: 660   Specia ...

  6. linux系统编程:read,write与lseek的综合应用

    这个实例根据命令行参数进行相应的读学操作: 用法: usage:./io file {r<length>|R<length>|w<string>|s<offs ...

  7. python+redis简单实现发红包程序

    redis是什么? Redis 是一个高性能的key-value数据库! 想进一步了解请移步搜索引擎自行查找. 编写这个小程序的目的就是对redis进行一个简单的小操作,对redis有一个初步的了解, ...

  8. js-权威指南学习笔记19.2

    1.jQuery动画是异步的,会立刻返回,但动画会在后台执行,可传入函数作为动画完成的回调函数. 2.jQuery动画默认是队列化的. 3.stop()方法接受两个可选的布尔值参数,如果第一个参数是t ...

  9. Maven学习(二)使用命令创建maven项目

    创建maven项目 手动 严格参照约定目录结构,我们开始手动新增文件夹 命令方式 project项目 我们也可以使用maven自动生成目录: mvn archetype:generate -Dgrou ...

  10. 解决Python 爬取ssh证书 的报错问题

    Python3 中会要求添加信任证书,但只是进行爬取数据就没必要了,我们可以忽略它 r1 =requests.get("https://www.baidu.com", verify ...