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


テーマリンク:http://www.patest.cn/contests/pat-a-practise/1065
タイトル:
Gven 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).The n T T test casesフォロワー,each consists of a single line containing three integers A,B and C,separated bysingspes.
Output Specification:
For each test case,output in one line「Case((zhi X:true)」if A+B>、or「Case(菗X:false)」otherswise,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
分析:
高精度の整数で作ったと思っていましたが、これは_u u u u u_uです.int 64で解決できるのは、オーバーフローに対する判定を加えるだけでよい.
その後問題が起きて、おばあさんに聞きました.
おばあちゃんの返事:レンジは減算で判断されます.例えばa<0 LL&&b<0 LL&&b<(long long long)0 x 800万LL-a
その後ずっと二つの答えが間違っていました.a+bをsumでしばらく預けてから解決します.
ACコード:
#include<stdio.h>
//#include <limits.h>     //             

bool check(long long a, long long b, long long c){
 /*
 if (a >= 0 && b >= 0) {
  if (a > LLONG_MAX - b) return true;
 }
 else if (a <= 0 && b <= 0) {
  if (a < LLONG_MIN - b) return false;
 }
 *///       
 long long sum;
 sum = a + b;
 if (a > 0 && b > 0 && sum <= 0) //        
  return true;
 else if (a < 0 && b < 0 && sum>= 0)//        
  return false;
 else
  return sum > c;
}
int main(void){
 freopen("F://Temp/input.txt", "r", stdin);
 int n;
 scanf("%d", &n);
 for (int i = 0; i < n; i ++){
  long long a, b, c;
  scanf("%lld%lld%lld", &a, &b, &c);
  printf("Case #%d: %s
", i + 1, (check(a, b, c) ? "true" : "false")); } return 0; }
スクリーンショット:
1065. A+B and C (64bit) (20)_第1张图片
——アプリ陳小旭