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. 编写Word宏使选中的图片改变为固定宽度并保持纵横比

    代码如下: Sub 改变图片宽度为12并设置灰色边框() ' ' 改变图片宽度为12并设置灰色边框 宏 ' '     ' Application.Run MacroName:="改变图片大 ...

  2. Asp.net Mvc 自定义Session (一),

    大家都知道用系统默认的session 会存在这样的问题 如果用户过多的话 session 会自动消亡,而且不能支持分布式和集群. 这系列博客主要讲解  怎样 解决用户过多的session自动消亡,和分 ...

  3. C#常用的集合类型(ArrayList类、Stack类、Queue类、Hashtable类、SortedList类)

    1.ArrayList类 ArrayList类主要用于对一个数组中的元素进行各种处理.在ArrayList中主要使用Add.Remove.RemoveAt.Insert四个方法对栈进行操作.Add方法 ...

  4. S2SH商用后台权限系统第三讲

    个位博友: 您好!今天我们做下登录页面,已经如何登录系统.我们的登录页面很简单,用户名.密码.验证码.下面首先描述下验证码的概念,验证码是为了防止机器人恶意登录.我们这里的验证码采用4位数字,当然你也 ...

  5. 数据库中substring的使用方法 CONVERT(varchar(12) , getdate(), 112 )

    Sqlserver中经常要操作一些时间类型的字段转换,我又不太记得住,所以搜集了以下的一些SqlserverConvertDateTime相关的资料发表在自己的小站里,方便自己以后要用的时候寻找,望对 ...

  6. [React] React Fundamentals: Owner Ownee Relationship

    The owner-ownee relationship is used to designate a parent-child relationship with React components ...

  7. 读完了csapp(中文名:深入理解计算机系统)

    上个星期最终把csapp看完了. 我买的是中文版的,由于除了貌似评价不错以外,由于涉及到些自己不了解的底层东西,怕是看英文会云里雾里.如今看来,大概不能算是个长处,可是的确可以加快我的看书速度,否则一 ...

  8. cocos2d-x之CCMotionStreak类——2013-08-25 16

      在游戏的实现过程中,有时会需要在某个游戏对象上的运动轨迹上实现渐隐效果.这种感觉就好像是类似飞机拉线的拖尾巴,在视觉上感觉很好,比如子弹的运动轨迹等,如果不借助引擎的帮助,这种效果往往需要通过大量 ...

  9. Day07 - Python 网络编程 Socket

    1. Python 网络编程 Python 提供了两个级别访问网络服务: 低级别的网络服务支持基本的 Socket,它提供了标准的 BSD Sockets API,可以访问底层操作系统Socket接口 ...

  10. 读写应用程序数据-SQLite3

    SQLite3是嵌入到ios中的关系型数据库.对存储大规模的数据非常实用,使得不必将每个对象加到内存中. 支持NULL.INTEGER.REAL(浮点数字).TEXT(字符串和文本).BLOB(二进制 ...