1065. A+B and C (64bit) (20)

时间限制 100 ms
内存限制 65536 kB
代码长度限制 16000 B
判题程序 Standard
作者 HOU, Qiming

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

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 是否成立的问题,唯一的坑点就是范围上线是 2^63 超过了计算机 long long 或  __int64 的上限( 2^63-1 )。所以无论在输入方面和计算方面都产生了一些障碍。
 
本弱的基础思想是用字符串读入所有数据,然后判断是否上溢出(这里指的是 long long 的上溢出,下文中亦如此),如果上溢出则进行特殊处理,否则正常处理。下面略具体的说下思路:(思路有可能会显得有些乱,但是绝对正确)
首先如果 a 和 b 都没有上溢出,那么求 a+b。如果上溢出必然 true,如果是下溢出必然 false,如果没有溢出则判断 c 是否上溢出,如果 c 上溢出则必然 false,否则正常比较处理即可。
如果 a 和 b 中有一个出现了上溢出,首先将上溢出的那一项挪至 a,方便后续处理。然后判断 c 是否上溢出,如果 c 上溢出则通过 b 的符号即可知 a+b>c 是否成立。如果 c 没有上溢出则通过 a*b 的符号判断,如果异号必然 a+b<=c,否则计算下 a+b 即可(具体小技巧看程序)。
另:本弱处理的不是特别优美,导致我需要单独特殊处理下 a 和 b 都是 -2^63 的情况。
 
特别注意!!!!!!!!!!!!!
 
网上很多很多的解题报告都是错的!!!!错在 2^63 + 2^63 > 1 这组数据上,然而该题的官方数据中没有此类数据,导致许多错误的程序都可以通过。
 
提供一组数据:

8
1 2 3
2 3 4
10 -10 0
101 -100 0
9223372036854775807 -9223372036854775808 0
9223372036854775808 9223372036854775808 1
9223372036854775808 -9223372036854775808 1
-9223372036854775808 -9223372036854775808 -1

ans:

Case #1: false
Case #2: true
Case #3: false
Case #4: true
Case #5: false
Case #6: true
Case #7: false
Case #8: false

代码:

 #include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const LL INF = 0x7FFFFFFFFFFFFFFF;
const int MAXN = ;
const char str[MAXN] = "";
const char st[MAXN] = "-9223372036854775808"; char a[MAXN], b[MAXN], c[MAXN];
LL na, nb, nc, nd; int check( LL x ) { return x > ? : x < ? - : ; } int main() {
int t, cnt = ;
int ta, tb, tc, td;
bool fa, fb, fc;
scanf( "%d", &t );
while( t-- ) {
printf( "Case #%d: ", ++cnt );
scanf( "%s%s%s", a, b, c );
if( !strcmp( a, st ) && !strcmp( b, st ) ) {
puts( "false" ); continue;
}
fa = !strcmp( a, str );
fb = !strcmp( b, str );
fc = !strcmp( c, str );
if( !fa && !fb ) {
sscanf( a, "%I64d", &na ); ta = check( na );
sscanf( b, "%I64d", &nb ); tb = check( nb );
if( !fc ) { sscanf( c, "%I64d", &nc ); tc = check( nc ); }
nd = na + nb; td = check( nd );
if( ( ta * tb ) > && ( ta * td ) < ) {
if( ta > ) puts( "true" );
else puts( "false" );
} else {
if( fc ) puts( "false" );
else puts( nd > nc ? "true" : "false" );
}
} else {
ta = a[] == '-' ? - : a[] == '' ? : ;
tb = b[] == '-' ? - : b[] == '' ? : ;
if( fc ) {
if( !fa ) { swap( a, b ); swap( ta, tb ); swap( fa, fb ); }
if( tb <= ) puts( "false" );
else puts( "true" );
} else {
sscanf( c, "%I64d", &nc ); tc = check( nc );
if( ta ^ tb ) {
if( !fa ) { swap( a, b ); swap( ta, tb ); swap( fa, fb ); }
sscanf( b, "%I64d", &nb );
nd = INF + nb + ;
puts( nd > nc ? "true" : "false" );
} else puts( "true" );
}
}
}
return ;
}

PAT 1065 A+B and C (64bit) (20)的更多相关文章

  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) (20 分)(溢出判断)*

    1065 A+B and C (64bit) (20 分)   Given three integers A, B and C in [−], you are supposed to tell whe ...

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

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

  5. PAT 甲级 1065. A+B and C (64bit) (20) 【大数加法】

    题目链接 https://www.patest.cn/contests/pat-a-practise/1065 思路 因为 a 和 b 都是 在 long long 范围内的 但是 a + b 可能会 ...

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

    题目 Given three integers A, B and C in [-263, 263], you are supposed to tell whether A+B > C. Inpu ...

  7. PAT (Advanced Level) 1065. A+B and C (64bit) (20)

    因为会溢出,因此判断条件需要转化.变成b>c-a #include<cstdio> #include<cstring> #include<cmath> #in ...

  8. PAT甲题题解-1065. A+B and C (64bit) (20)-大数溢出

    第一眼以为是大数据,想套个大数据模板,后来发现不需要.因为A.B.C的大小为[-2^63, 2^63],用long long 存储他们的值和sum. 接下来就是分类讨论:如果A > 0, B & ...

  9. PAT Advanced 1065 A+B and C (64bit) (20 分)(关于g++和clang++修改后能使用)

    Given three integers A, B and C in [−], you are supposed to tell whether A+B>C. Input Specificati ...

随机推荐

  1. 初次接触Android ActionBar比较烦人的问题[转]

    本文转自:http://blog.csdn.net/u010933209/article/details/40112079 问题一:icon不能正常显示 一直都对actionbar又爱又恨,特别是刚接 ...

  2. Eclipse的下载、安装和WordCount的初步使用(本地模式和集群模式)

    包括:    Eclipse的下载 Eclipse的安装 Eclipse的使用 本地模式或集群模式 Scala IDE for Eclipse的下载.安装和WordCount的初步使用(本地模式和集群 ...

  3. PTA 08-图7 公路村村通 (30分)

    现有村落间道路的统计数据表中,列出了有可能建设成标准公路的若干条道路的成本,求使每个村落都有公路连通所需要的最低成本. 输入格式: 输入数据包括城镇数目正整数NN(\le 1000≤1000)和候选道 ...

  4. [置顶] 项目进阶 之 持续构建环境搭建(二)Nexus私服器

    上一篇博文项目进阶 之 持续构建环境搭建(一)架构中,我们大致讲解了一下本系列所搭建环境的基本框架,这次开始我们进入真正的环境搭建实战.重点不在于搭建的环境是否成功和完善,而是在搭建过程中充分认识到每 ...

  5. RHCA学习笔记:RH442-Unit9内核定时与进程延时

      Unit 9 Kernel Timing and Process Latency 内核定时与进程延时 学习目标: A.了解CPU 是怎样追踪时间的 B.调整CPU的访问次数 C.调整调度延时 D. ...

  6. 标准I/O库之标准I/O的效率

    程序清单5-1 用getc和putc将标准输入复制到标准输出 #include "apue.h" int main( void ) { int c; while(( c = get ...

  7. c# 可以有多个Main()函数

    可以有多个Main()函数,这样写:namespace ConsoleApp1{class Program{static void Main(string[] args){Console.WriteL ...

  8. 动作之CCActionInstant(立即动作)家族

    立即动作就是不需要时间,马上就完成的动作.立即动作的共同基类是CCActionInstant.CCActionInstant的常用子类有: CCCallFunc:回调函数包装器 CCFlipX:X轴翻 ...

  9. Cocos-2d 坐标系及其坐标转换

    Cocos-2d中,涉及到4种坐标系: GL坐标系Cocos2D以OpenglES为图形库,所以它使用OpenglES坐标系.GL坐标系原点在屏幕左下角,x轴向右,y轴向上. 屏幕坐标系苹果的Quar ...

  10. linux shell read command-Getting User Input Via Keyboard--ref

    ref:http://bash.cyberciti.biz/guide/Getting_User_Input_Via_Keyboard You can accept input from the ke ...