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

Source: Internet
Author: User

1065. A+b and C (64bit) (20) time limit MS Memory limit 65536 KB code length limit 16000 B Award Program StandardAuthor HOU, qiming

Given three integers A, B and C in [ -263, 263], you is 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 num ber (starting from 1).

Sample Input:
31 2 32 3 49223372036854775807-9223372036854775808 0
Sample Output:
Case #1: falsecase #2: truecase #3: False
Thinking and Analysis:This question is simply a simple ruminate a+b>c whether the problem is established, the only pit point is that the range is 2^63 over the computer long long or the upper limit of __int64 (2^63-1). As a result, there are some obstacles in terms of both input and computation. The basic idea of the weak is to read all the data in a string, and then determine whether the overflow (this refers to a long long overflow, also below), if the overflow is special processing, otherwise normal processing. The following is a slightly specific idea: (ideas may seem a bit messy, but absolutely correct)first, if both A and B are not overflow, then seek a+b. If the overflow must be true, if the overflow must be false, if there is no overflow to determine whether C overflow, if the C overflow is necessarily false, otherwise normal comparison processing can be. If there is an overflow in a and B, the first item on the overflow is moved to a to facilitate subsequent processing. Then determine if C is overflow, if the C overflow through the B symbol can know whether A+B&GT;C is established. If C does not overflow is judged by the a*b symbol, if the a+b<=c is inevitable, otherwise the calculation a+b can (specific small skills to see the program). another:This weak treatment is not particularly beautiful, resulting in my need for special treatment under A and B are -2^63 situation. Special attention!!!!!!!!!!!!! Many many of the online problem-solving reports are wrong!!! Wrong in 2^63 + 2^63 > 1 This set of data, however, the title of the official data does not have such data, resulting in many wrong procedures can be passed. provide a set of data:

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

Code:

1#include <bits/stdc++.h>2 using namespacestd;3typedefLong LongLL;4 ConstLL INF =0x7fffffffffffffff;5 Const intMAXN = -;6 Const CharSTR[MAXN] ="9223372036854775808";7 Const CharST[MAXN] ="-9223372036854775808";8 9 CharA[MAXN], B[MAXN], C[MAXN];Ten LL na, NB, NC, ND; One  A intCheck (LL x) {returnX >0?1: x <0? -1:0; } -  - intMain () { the     intT, cnt =0; -     intTA, TB, TC, TD; -     BOOLFA, FB, FC; -scanf"%d", &t); +      while(t-- ) { -printf"Case #%d:", ++CNT); +scanf"%s%s%s", A, b, c); A         if(!STRCMP (A, st) &&!strcmp (b, ST)) { atPuts"false");Continue; -         } -FA =!strcmp (A, str); -FB =!strcmp (b, str); -FC =!strcmp (c, str); -         if(!FA &&!)FB) { inSSCANF (A,"%i64d", &na); TA =check (NA); -SSCANF (b,"%i64d", &AMP;NB); TB =check (NB); to             if(!FC) {sscanf (c,"%i64d", &AMP;NC); TC =Check (NC);} +nd = Na + nb; TD =check (ND); -             if(TA * tb) >0&& (TA * td) <0 ) { the                 if(Ta >0) puts ("true" ); *                 ElsePuts"false" ); $}Else {Panax Notoginseng                 if(FC) puts ("false" ); -                 ElsePuts (nd > NC?)"true":"false" ); the             } +}Else { ATA = a[0] =='-'? -1: a[0] =='0'?0:1; theTB = b[0] =='-'? -1: b[0] =='0'?0:1; +             if(FC) { -                 if( !FA) {Swap (A, b); swap (TA, TB); Swap (FA, FB);} $                 if(TB <=0) puts ("false" ); $                 ElsePuts"true" ); -}Else { -SSCANF (c,"%i64d", &AMP;NC); TC =Check (NC); the                 if(Ta ^TB) { -                     if( !FA) {Swap (A, b); swap (TA, TB); Swap (FA, FB);}WuyiSSCANF (b,"%i64d", &nb); thend = INF + NB +1; -Puts (nd > NC?)"true":"false" ); Wu}ElsePuts"true" ); -             } About         } $     } -     return 0; -}
View Code

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.