1065 A+B and C (64bit) (20point(s)) Easy only once *机器定点数补码的问题

基本思想:

题目中提示了,注意int 和long long的数据大小,int四个字节,long long8个字节,所以一个正负2^31,一个正负2^63,范围要注意;

关键点:

最后一个测试点卡在了负数溢出判断上。根据补码的问题,64位补码最小负数为10000....0000,即-2^63,所以两个相加为0000...0000,所以两个负数相加溢出包括0;

 1 #include<iostream>
 2 #include<stdlib.h>
 3 #include<stdio.h>
 4 #include<vector> 
 5 #include<string>
 6 #include<math.h>
 7 #include<algorithm>
 8 using namespace std;
 9 using std::vector; 
10 typedef long long ll;
11 int main() {
12     ll a, b, c;
13     int n;
14     scanf("%d", &n);
15     for (int i = 0; i < n; i++) {
16         scanf("%lld %lld %lld", &a, &b, &c);
17         ll d = a + b;
18         //cout << "reasult:" << d << endl;
19         if (a > 0 && b > 0) {
20             if (d < 0)
21                 printf("Case #%d: true\n", i + 1);
22             else if (d > c) {
23                 printf("Case #%d: true\n", i + 1);
24             }
25             else {
26                 printf("Case #%d: false\n", i + 1);
27             }
28         }
29         else if (a < 0 && b < 0) {
30             if (d >= 0)
31                 printf("Case #%d: false\n", i + 1);
32             else if (d > c) {
33                 printf("Case #%d: true\n", i + 1);
34             }
35             else {
36                 printf("Case #%d: false\n", i + 1);
37             }
38         }
39         else {
40             if (d > c) {
41                 printf("Case #%d: true\n", i + 1);
42             }
43             else {
44                 printf("Case #%d: false\n", i + 1);
45             }
46         }
47     }
48     system("pause");
49     return 0;
50 }

猜你喜欢

转载自www.cnblogs.com/songlinxuan/p/12187619.html