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. poj 2367 Genealogical tree (拓扑排序)

    火星人的血缘关系很奇怪,一个人可以有很多父亲,当然一个人也可以有很多孩子.有些时候分不清辈分会产生一些尴尬.所以写个程序来让n个人排序,长辈排在晚辈前面. 输入:N 代表n个人 1~n 接下来n行 第 ...

  2. (qsf文件 、 tcl文件 和 csv(txt)文件的区别) FPGA管脚分配文件保存、导入导出方法

    FPGA管脚分配文件保存方法 使用别人的工程时,有时找不到他的管脚文件,但可以把他已经绑定好的管脚保存下来,输出到文件里. 方法一: 查看引脚绑定情况,quartus -> assignment ...

  3. hdoj 1406 完数

    完数 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submissi ...

  4. 在配置XML时报的The reference to entity "dataSource" must end with the ';' delimiter错误

    <?xml version="1.0" encoding="UTF-8"?> <ECharts> <element>http ...

  5. SQL Server数据库PIVOT函数的使用详解(一)

    http://database.51cto.com/art/201108/285250.htm SQL Server数据库中,PIVOT在帮助中这样描述滴:可以使用 PIVOT 和UNPIVOT 关系 ...

  6. Objective C 代码片段(类别)

    @interface NSString (reverse) -(NSString *) reverseString; @end @implementation NSString (reverse) - ...

  7. Java基础知识强化之IO流笔记43:IO流练习之 复制文本文件的 5 种方式案例

     1. 案例分析: 分析: 复制数据,如果我们知道用记事本打开并能够读懂,就用字符流,否则用字节流. 通过该原理,我们知道我们应该采用字符流更方便一些. 而字符流有5种方式,所以做这个题目我们有5种方 ...

  8. Oracle 流式制造功能培训

    转自百度文库: http://wenku.baidu.com/link?url=dRyll_P7C3fepoUp5dggYVzw6lVmifwEJMLvis1CN58m09WYF1unY3Ddn9Lq ...

  9. c#的方法重写和的java方法重写有什么区别

    java code: package example; class m1 { public int getInt() { return 0; } } class m2 extends m1 { pub ...

  10. centos6 install mplayer(multimedia)

    step_1 http://wiki.centos.org/AdditionalResources/Repositories/RPMForge step_2 http://wiki.centos.or ...