• PAT 1065 A+B and C (64bit)


    1065 A+B and C (64bit)

    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). Each line should ends with '\n'.

    Sample Input:

    1. 3
    2. 1 2 3
    3. 2 3 4
    4. 9223372036854775807 -9223372036854775808 0

    Sample Output:

    1. Case #1: false
    2. Case #2: true
    3. Case #3: false

    总结:因为 a b c 的取值范围是[-2^6,2^63],所以当 a b 同号的时候可能会发生溢出的情况,需要额外判断

    自己的做法:利用pow(2,63)-1表示最大值与 a b相加减来判断 a b 是否越界了(也不知道是哪里出错了)

    正号:a<0 && b<0  && pow(2,63)-1+a+b<0表示两数相加越界了

    符号:a>0 && b>0  && pow(2,63)+a+b<0表示两数相加越界了

    1. #include
    2. #include
    3. using namespace std;
    4. int main(){
    5. int n;
    6. scanf("%d",&n);
    7. for(int i=1;i<=n;i++){
    8. long long a,b,c;
    9. scanf("%lld%lld%lld",&a,&b,&c);
    10. if(a<0 && b<0 && pow(2,63)+a+b<0) printf("Case #%d: false\n",i);
    11. else if(a>0 && b>0 && pow(2,63)-1-a-b<0) printf("Case #%d: true\n",i);
    12. else if(a+b>c) printf("Case #%d: true\n",i);
    13. else printf("Case #%d: false\n",i);
    14. }
    15. return 0;
    16. }

    代码:

    1. #include
    2. using namespace std;
    3. int main() {
    4. int n;
    5. scanf("%d", &n);
    6. for(int i = 0; i < n; i++) {
    7. long long a, b, c;
    8. scanf("%lld %lld %lld", &a, &b, &c);
    9. long long sum = a + b;
    10. if(a > 0 && b > 0 && sum < 0) {
    11. printf("Case #%d: true\n", i + 1);
    12. } else if(a < 0 && b < 0 && sum >= 0){
    13. printf("Case #%d: false\n", i + 1);
    14. } else if(sum > c) {
    15. printf("Case #%d: true\n", i + 1);
    16. } else {
    17. printf("Case #%d: false\n", i + 1);
    18. }
    19. }
    20. return 0;
    21. }

    好好学习,天天向上!

    我要考研

  • 相关阅读:
    代码随想录算法训练营 动态规划part08
    AI赋能Oracle DBA:以自然语言与Oracle数据库互动
    流辰信息助力企业数字化转型
    Mac 部署 GPT-2 预训练模型 gpt2-chinese-cluecorpussmall
    基于单片机16位智能抢答器设计
    视频分析:走路看手机行为
    大数据之路阿里巴巴实践
    Java:异常
    myBatis基础学习笔记
    【每日一题】回文子串的最小切割数
  • 原文地址:https://blog.csdn.net/weixin_50679551/article/details/127121605