码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 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. }

    好好学习,天天向上!

    我要考研!

  • 相关阅读:
    Hello CTP(一)——期货业务
    热烈祝贺方正璞华两款产品入选2021年度江苏省工业软件优秀产品和应用解决方案拟推广名单
    3. 双向约瑟夫问题
    Vue47-修改默认配置webpack.config.js文件
    腾讯云服务器秒杀什么时候开始?腾讯云服务器秒杀时间
    堆--数组中第K大元素
    【微信小程序】好看的轮播图组件
    为什么红黑树比AVL树效率高?
    日本知名汽车零部件公司巡礼系列之株式会社123
    http和https的区别在哪
  • 原文地址:https://blog.csdn.net/weixin_50679551/article/details/127121605
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号