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

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

代码:

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 typedef long long LL;
 4 const LL INF = 0x7FFFFFFFFFFFFFFF;
 5 const int MAXN = 30;
 6 const char str[MAXN] = "9223372036854775808";
 7 const char st[MAXN] = "-9223372036854775808";
 8
 9 char a[MAXN], b[MAXN], c[MAXN];
10 LL na, nb, nc, nd;
11
12 int check( LL x ) { return x > 0 ? 1 : x < 0 ? -1 : 0; }
13
14 int main() {
15     int t, cnt = 0;
16     int ta, tb, tc, td;
17     bool fa, fb, fc;
18     scanf( "%d", &t );
19     while( t-- ) {
20         printf( "Case #%d: ", ++cnt );
21         scanf( "%s%s%s", a, b, c );
22         if( !strcmp( a, st ) && !strcmp( b, st ) ) {
23             puts( "false" ); continue;
24         }
25         fa = !strcmp( a, str );
26         fb = !strcmp( b, str );
27         fc = !strcmp( c, str );
28         if( !fa && !fb ) {
29             sscanf( a, "%I64d", &na ); ta = check( na );
30             sscanf( b, "%I64d", &nb ); tb = check( nb );
31             if( !fc ) { sscanf( c, "%I64d", &nc ); tc = check( nc ); }
32             nd = na + nb; td = check( nd );
33             if( ( ta * tb ) > 0 && ( ta * td ) < 0 ) {
34                 if( ta > 0 ) puts( "true" );
35                 else puts( "false" );
36             } else {
37                 if( fc ) puts( "false" );
38                 else puts( nd > nc ? "true" : "false" );
39             }
40         } else {
41             ta = a[0] == ‘-‘ ? -1 : a[0] == ‘0‘ ? 0 : 1;
42             tb = b[0] == ‘-‘ ? -1 : b[0] == ‘0‘ ? 0 : 1;
43             if( fc ) {
44                 if( !fa ) { swap( a, b ); swap( ta, tb ); swap( fa, fb ); }
45                 if( tb <= 0 ) puts( "false" );
46                 else puts( "true" );
47             } else {
48                 sscanf( c, "%I64d", &nc ); tc = check( nc );
49                 if( ta ^ tb ) {
50                     if( !fa ) { swap( a, b ); swap( ta, tb ); swap( fa, fb ); }
51                     sscanf( b, "%I64d", &nb );
52                     nd = INF + nb + 1;
53                     puts( nd > nc ? "true" : "false" );
54                 } else puts( "true" );
55             }
56         }
57     }
58     return 0;
59 }

时间: 2024-10-10 23:28:40

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

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

1 #include <stdio.h> 2 3 int main() { 4 long n, a, b, c; 5 long i; 6 int ga, gb, gc, r; 7 scanf("%ld", &n); 8 for (i=0; i<n; i++) { 9 scanf("%ld%ld%ld", &a, &b, &c); 10 ga = a >= 0; 11 gb = b >= 0; 12 gc

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

因为会溢出,因此判断条件需要转化.变成b>c-a #include<cstdio> #include<cstring> #include<cmath> #include<vector> #include<queue> #include<algorithm> using namespace std; long long a,b,c; int main() { int T; scanf("%d",&T);

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

第一眼以为是大数据,想套个大数据模板,后来发现不需要.因为A.B.C的大小为[-2^63, 2^63],用long long 存储他们的值和sum. 接下来就是分类讨论:如果A > 0, B < 0 或者 A < 0, B > 0,sum不会溢出,直接和C判断比较即可.如果A > 0, B > 0,sum可能会溢出, 溢出的话为负数,所以sum < 0时候说明溢出了,那么肯定是大于C的.如果A < 0, B < 0,sum可能会溢出,同理,sum &g

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

#include<stdio.h> #include<stdlib.h> 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); long long sum=a+b; //[思维]:判断sum是否正负溢出,溢出的话不能直接按计算判断! i

PAT Advanced 1065 A+B and C (64bit) (20分)

Given three integers A, B and C in [−], 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 (≤). Then T test cases follow, each consists of a single line containing three

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

1065 A+B and C (64bit) (20分)(水)

Given three integers A, B and C in [−], 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 (≤). Then T test cases follow, each consists of a single line containing three

1065 A+B and C (64bit) (20)(大数相加、正溢出、负溢出)

正溢出:两个正数相加超过了该数据类型能表示的最大范围,结果为负数 负溢出:两个负数相加超过了该数据类型能表示的最小范围,结果为正数包括零 #include <algorithm> #include <iostream> #include <cstdio> #include <queue> #include <cstring> #include <vector> using namespace std; const int maxn =

1065 A+B and C (64bit) (20 分)大数 溢出

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