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. laravel 单词

    return view('admin.user.login'); 返回 admin文件夹下, user文件夹中 login文件模板 setcookie 语法 setcookie(name,value, ...

  2. Android获取SD卡总容量,可用大小,机身内存总容量及可用大小

    public long getSDTotalSize() { /*获取存储卡路径*/ File sdcardDir= Environment.getExternalStorageDirectory() ...

  3. Hibernate高效查询,只查询部分/指定字段

    公司使用 DetachedCriteria detachedCriteria = DetachedCriteria.forClass(PeBulletin.class); detachedCriter ...

  4. (一)java并发知识图谱

  5. git使用基本教程

    黑马的视频,以前看过廖雪峰的git,总是学不懂,这次终于看会了,结合视频更佳,红色字是重点. 基于linux下面git百度云视频教程:http://pan.baidu.com/s/1bpk472B 密 ...

  6. 过滤器模式(Filter Pattern)

    过滤器模式 一.什么是过滤器模式   过滤器模式(Filter Pattern),这种模式允许开发人员使用不同的标准来过滤一组对象,通过逻辑运算以解耦的方式把它们连接起来.这种类型的设计模式属于结构型 ...

  7. Incircle and Circumcircle(二分+几何)浙大月赛zoj3806(详解版)图

    Incircle and Circumcircle Time Limit: 2 Seconds Memory Limit: 65536 KB Special Judge A triangle is o ...

  8. 如何在SpringMVC中使用REST风格的url

    如何在SpringMVC中使用REST风格的url 1.url写法: get:/restUrl/{id} post:/restUrl delete:/restUrl/{id} put:/restUrl ...

  9. python正则表达式3-模式匹配

    re.S,使 '.'  匹配换行在内的所有字符 >>> pattern=r'ghostwu.com' >>> import re >>> re.f ...

  10. POJ2104(可持久化线段树)

    K-th Number Time Limit: 20000MS   Memory Limit: 65536K Total Submissions: 58759   Accepted: 20392 Ca ...