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

题目分析:利用long long 存储数据 但是要注意对于num的判断 因为long long 最大存储范围是从负2的63次 到 正2的63次减一
如果这个和大于或小于这个范围 就要先判断再输出
对于这道题来说
数的范围是 $[-2^63,2^63]$ 而longlong的范围是$[-2^63,2^63-1]$
若两个数的和过大到区间$[2^63,2^64-2] 那么会溢出改变到区间 [-2^63,-2](对于非浮点数来说上界加一会变为加一后的值取负 故(2^63-1+1)会变为-2^63 -2是根据公式 (2^64-2)%2^64=-2求得$
同理 若过小到区间$[-2^64,-2^63-1] 会溢出改变到区间[0,2^63-1]$

 1 #define _CRT_SECURE_NO_WARNINGS
 2 #include <climits>
 3 #include<iostream>
 4 #include<vector>
 5 #include<queue>
 6 #include<map>
 7 #include<set>
 8 #include<stack>
 9 #include<algorithm>
10 #include<string>
11 #include<cmath>
12 using namespace std;
13 string A, B, C;
14
15 int main()
16 {
17     int N;
18     cin >> N;
19     for (int i = 1; i <= N; i++)
20     {
21         long long A, B, C;
22         cin >> A >> B >> C;
23         long long num = A + B;
24         if (A > 0 && B > 0 && num < 0)
25             printf("Case #%d: true\n", i);
26         else if (A < 0 && B < 0 && num >= 0)
27             printf("Case #%d: false\n", i);
28         else if(num > C)
29             printf("Case #%d: true\n", i);
30         else
31             printf("Case #%d: false\n", i);
32     }
33 }

原文地址:https://www.cnblogs.com/57one/p/12067988.html

时间: 2024-07-29 15:19:03

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

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

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 po

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

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

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 =

1058 A+B in Hogwarts (20分)(水)

If you are a fan of Harry Potter, you would know the world of magic has its own currency system -- as Hagrid explained it to Harry, "Seventeen silver Sickles to a Galleon and twenty-nine Knuts to a Sickle, it's easy enough." Your job is to write

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