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

作者: HOU, Qiming

单位: 浙江大学

时间限制: 400 ms

内存限制: 64 MB

代码长度限制: 16 KB

法一:用高精度去计算

#include <bits/stdc++.h>
using namespace std;int main(){int n=0;cin>>n;for(int j=1;j<=n;j++){string a,b,c,d;cin>>a>>b>>c;if(a.size()<b.size())swap(a, b);vector<int>ai(0),bi(0),ci(0);int fa=1,fb=1,fc=1; //1为正数,0为负数if(a[0]=='-')fa=0;if(b[0]=='-')fb=0;if(c[0]=='-')fc=0;for(int i=a.size()-1;i>=0;i--){if(fa)ai.push_back(a[i]-'0');else if(i!=0)ai.push_back(-(a[i]-'0'));}for(int i=b.size()-1;i>=0;i--){if(fb)bi.push_back(b[i]-'0');else if(i!=0)bi.push_back(-(b[i]-'0'));}for(int i=0;i<bi.size();i++)ai[i]+=bi[i];for(int i=0;i<ai.size();i++)if(ai[i]<0&&i!=ai.size()-1){ai[i]+=10;ai[i+1]--;}else if(ai[i]>9&&i<ai.size()-1){ai[i]-=10;ai[i+1]++;}else if(i==ai.size()-1&&ai[i]>9){ai[i]-=10;ai.push_back(1);}if(ai[ai.size()-1]<0)fa=0;if(fa==1&&fc==0){cout<<"Case #"<<j<<": true"<<endl;continue;}else if(fa==0&&fc==1){cout<<"Case #"<<j<<": false"<<endl;continue;}else if(fa==fc&&fa==1){if(ai.size()>c.size()){cout<<"Case #"<<j<<": true"<<endl;continue;}else if(ai.size()<c.size()){cout<<"Case #"<<j<<": false"<<endl;continue;}int i=0;while (ai[ai.size()-1-i]==c[i]-'0'&&i<ai.size()) i++;if(i<c.size()&&ai[ai.size()-1-i]>c[i]-'0'){cout<<"Case #"<<j<<": true"<<endl;continue;}else {cout<<"Case #"<<j<<": false"<<endl;continue;}}else {if(ai.size()>c.size()){cout<<"Case #"<<j<<": false"<<endl;continue;}else if(ai.size()<c.size()){cout<<"Case #"<<j<<": true"<<endl;continue;}int i=0;while (ai[ai.size()-1-i]==c[i]-'0'&&i<ai.size()) i++;if(i<c.size()&&ai[ai.size()-1-i]<c[i]-'0'){cout<<"Case #"<<j<<": true"<<endl;continue;}else {cout<<"Case #"<<j<<": false"<<endl;continue;}}}return 0;
}

法二:用long long加一点技巧

#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <vector>
using namespace std;int main()
{int n;scanf("%d",&n);for(int i=1;i<=n;i++){long long a,b,c;scanf("%lld %lld %lld",&a,&b,&c);bool istrue;long long res=a+b;if(a>0&&b>0&&res<=0) istrue=true;else if(a<0&&b<0&&res>=0) istrue=false;else if(res>c) istrue=true;else istrue=false;printf("Case #%d: %s\n",i,istrue?"true":"false");}
}

法三:java的BigInteger


import java.math.BigInteger;
import java.util.Scanner;public class Main {public static void main(String[] args) {// TODO Auto-generated method stubScanner sc = new Scanner(System.in);int n = sc.nextInt();for(int i=0;i<n;i++) {BigInteger a = sc.nextBigInteger();BigInteger b = sc.nextBigInteger();BigInteger c = sc.nextBigInteger();a=a.add(b);if(a.compareTo(c)>0)System.out.println("Case #"+(i+1)+": true");else System.out.println("Case #"+(i+1)+": false");}}
}

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. PATA 1065 A+B and C (64bit) (20分)

    PATA 1065 A+B and C (64bit) (20分) 题目描述:Given three integers A, B and C in [−2^​63, 2^​63], you are s ...

  3. PAT甲级1065 A+B and C (64bit):[C++题解]爆long long,熟悉计算机存储有符号数原理

    文章目录 题目分析 题目来源 题目分析 来源:acwing 一般64位机上,long long 是64位.所以263−12^{63}−1263−1是long long 的范围,所以a+b可能会爆lon ...

  4. 1065 A+B and C (64bit) (20 分)【难度: 简单 / 思维 高精度】

    https://pintia.cn/problem-sets/994805342720868352/problems/994805406352654336 方法一: 用高精度的板子写一下是可以的,不过 ...

  5. 【PAT甲级 大数运算】1065 A+B and C (64bit) (20 分) Python 全部AC

    题目 初学python,第一次用python刷oj,挑了个简单题试试手. 在大数运算方面,python没有数的大小限制,简直开挂. total = int(input()) for i in rang ...

  6. 【PAT甲级 大整数BigInteger】1065 A+B and C (64bit) (20 分) Java 全部AC

    题目 在有些方面,比如大整数的处理,不得不佩服Java,好用没的说,像开挂一样 题解 Java import java.math.BigInteger; import java.util.Scanne ...

  7. 【详细讲解】1065 A+B and C (64bit) (20 分)

    立志用最少的代码做最高效的表达 PAT甲级最优题解-->传送门 Given three integers A, B and C in [−2^​63​​ ,2^​63​​ ], you are ...

  8. PAT 1065 A+B and C[大数运算][溢出]

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

  9. PAT甲级题目翻译+答案 AcWing(模拟)

    1008 Elevator (20 分) 思路 :last可能等于cur,而无论是否相等,res都是+5的 #include <iostream>using namespace std;i ...

最新文章

  1. 麦肯锡顾问的整体设计:从大局需要安排工作
  2. 管理95后员工,管理者必知的4条底层逻辑
  3. 物联网设计四大独特挑战的解决方案
  4. shell scripts 之 代码量统计
  5. 公司 邮件 翻译 培训 长难句 结课
  6. PHP语言的RSA算法加解密程序
  7. 支持向量机的前世与今生
  8. 深入理解计算机系统第四版_技术干货 |深入理解计算机系统之链接
  9. matlab基于瑞利信道,基于matlab的瑞利信道仿真.docx
  10. kettle简单的更新与插入
  11. 泛雅计算机网络,2020超星泛雅网课计算机网络技术最新最全见面课答案
  12. vue对象属性为null_vue 解决无法对未定义的值,空值或基元值设置反应属性报错问题...
  13. SpringBoot如何自定义启动的Banner 在线生成SpringBoot启动的Banner工具 如何使用在线生成工具生成的SpringBoot的Banner
  14. Vue项目实战——实现一个任务清单【基于 Vue3.x 全家桶(简易版)】
  15. 关于汇编语言中的立即寻址和直接寻址
  16. Redis看这一篇就够了
  17. 计算机应用基础0006 19春在线作业1,川大《计算机应用基础0006》20春在线作业1 -0001...
  18. Django Restframework 里的小插曲
  19. java字符串加密解密工具类
  20. html情侣计时器,手机桌面恋爱计时器

热门文章

  1. input宽度随内容变化
  2. 1、Lock接口以及ReentrantLock可重入锁
  3. MAC本地服务器搭建
  4. Intellij IDEA激活服务器
  5. 告别暗黄皮肤变水嫩皮肤的8个小习惯
  6. Expert C Programming 阅读笔记(~CH1)
  7. 6.4 Web安全漏洞学习平台:WebGoat的使用
  8. ckpt转bin模型报错解决:AttributeError: ‘BertForPreTraining‘ object has no attribute ‘shape‘ #393
  9. 使用funshionchart技术总结
  10. huya live source、douyu