PAT - 1065 A+B and C (64bit) (20)

Given three integers A, B and C in [-2^63^, 2^63^], you are supposed to tell whether A+B > C.

Input

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

For each test case, output in one line “Case #X: true” if A+B&gtC, 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, C, 问 A+B 是否大于 C
【数据范围】
-263 <= A, B, C <= 263
注意:经(晴神)测试,测评数据中不存在 A 或 B 取 263 的情况,数据范围应为-263 <= A, B, C < 263,即左闭右开。
因为 263 存储会自动变成 -263,无法区分左右边界。
【解题思路】
第一反应是数组模拟大数加减(天真
看了算法笔记,考察溢出。
1)正常,没有出现溢出的情况。直接判断 A+B > C 即可
2)正溢出(即 A+B>=263),A > 0 && B > 0 && A+B <= -2。
由于 A, B 的最大值是 263-1,A+B 最大值 264-2,得溢出范围[264-2, (264-2)%264],即[264-2, -2]。
3)负溢出(即 A+B<-263),A < 0 && B < 0 && A+B >= 0。
由于 A, B 的最小值是 263,A+B 最小值 264,得溢出范围[(-264)%264, -264],即[0, 264]。
【注意】
A + B 要先计算出然后赋值到一个变量里,再去和 C 比较,否则会答案错误。

然而晴神并没有解释为什么!想知道为什么就故意写了没有中间变量的代码丢给牛客看返回结果,结果牛客上 AC 了???要么数据不一样,要么就是测评姬的问题惹……知道为什么会错误的请告诉我!


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int INF = 0x3f3f3f3f;
const int MAXN = 100000+10;
ll a, b, t, c;
int T;
int main() {
    scanf("%d", &T);
    for (int i = 1; i <= T; i++) {
        scanf("%lld%lld%lld", &a, &b, &c);
        ll tmp = a + b;
        if (a>0&&b>0&&tmp<=-2) printf("Case #%d: true\n", i);
        else if (a<0&&b<0&&tmp>=0) printf("Case #%d: false\n", i);
        else if (tmp > c)  printf("Case #%d: true\n", i);
        else printf("Case #%d: false\n", i);
    } 

return 0;
}   else printf("Case #%d: false\n", i);
    } 

return 0;
}

看完了分析来看一个暴力的:
因为 long long 和 double 存储方式不同,虽然都是 8 个字节但是 double 的可存储数字范围要比 long long 大,但是由于精度问题,会出现误差。long double 类型没有具体的标准,但是要比 double 大,注意 %llf,水过~

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int INF = 0x3f3f3f3f;
const int MAXN = 100000+10;
long double a, b, c;
int T; 
int main() {
    scanf("%d", &T);
    for (int i = 1; i <= T; i++) {
        scanf("%llf%llf%llf", &a, &b, &c);
        printf("Case #%d: ", i);
        printf(a+b>c?"true\n":"false\n");
    }
return 0;
}

提交时间状态分数题目编译器耗时
2018/7/24 22:31:08答案正确201065C++ (g++)4 ms
2018/7/24 23:35:36答案正确201065C++ (g++)4 ms
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值