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). Each line should ends with '\n'
.
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三个数,判断A+B>C为true还是false
思路:1、本人思路:注意A、B、C的范围为(−2^63,2^63),即使long long int可以存下,但是A+B的结果却可以不在long long int的存储范围内,因此A、B、C无法用整型存,只能用字符串存储,这就涉及到大数加法,而正数和负数相加就是大数减法,运用大数加减法就能计算A+B-C得到结果,如果结果字符串第一位为‘-’,就说明A+B>C为true,反之为false,代码写下来挺长的
2、柳神思路:对A+B溢出的情况分类讨论,20分的题,理应这样,是我想的太复杂了。
因为A、B的范围为(−2^63,2^63),A+B的结果就有可能溢出,对溢出的结果进行分析即可得出A+B的结果估值
(1)如果A,B中有一个为正数、一个为负数,那么A+B就不可能溢出,直接判断A+B>C是否为真即可
(2)如果A>0 并且 B>0,那么A+B的结果为负的话,就说明溢出了,而且足以说明A+B>C
(3)如果A<0并且B<0,那么A+B的结果为正的话,就说明溢出了,而且足以说明A+B<C
AC代码(本人所写,利用了大数加法和大数减法)
#include <iostream>
#include <string>
using namespace std;
string add(string a, string b) {
int len_max = a.length() > b.length() ? a.length() : b.length();
int len_min = a.length() < b.length() ? a.length() : b.length();
string c = a.length() > b.length() ? a : b;
int i = 0, carry = 0;
while (i < len_max) {
int temp = 0;
if (i < len_min) {
temp += a[a.length() - i - 1] + b[b.length() - i - 1] - 2 * '0';
} else {
temp += c[len_max - i - 1] - '0';
}
if (carry != 0) {
temp += carry;
carry = 0;
}
if (temp >= 10) {
c[len_max - i - 1] = temp - 10 + '0';
carry = 1;
} else {
c[len_max - i - 1] = temp + '0';
}
i++;
}
if (carry != 0) {
c = '1' + c;
}
return c;
}
string Minus(string a, string b) {
int i = 0, borrow = 0;
string c = a;
while (i < a.length()) {
int temp = a[a.length() - i -1] - '0';
if (borrow != 0) {
temp -= 1;
borrow = 0;
}
if (i < b.length()) {
temp -= b[b.length() - i - 1] -'0';
}
if (temp < 0) {
borrow = 1;
temp += 10;
}
c[c.length() - i - 1] = temp + '0';
i++;
}
if (borrow != 0) {
c[0] = c[0] - 1;
}
return c;
}
bool cmp(string a, string b) {
if (a.length() > b.length()) {
return true;
} else if (a.length() == b.length() && a > b) {
return true;
} else {
return false;
}
}
string calculate(string a, string b) {
string c;
if (a[0] == '-' && b[0] == '-') {
c= '-' + add(a.substr(1, string::npos), b.substr(1, string::npos));
} else if (a[0] != '-' && b[0] != '-') {
c= add(a, b);
} else if (a[0] == '-' && b[0] != '-') {
if (cmp(a.substr(1, string::npos), b)) {
c = '-';
c += Minus(a.substr(1, string::npos), b);
} else {
c += Minus(b, a.substr(1, string::npos));
}
} else {
if (cmp(a, b.substr(1, string::npos))) {
c += Minus(a, b.substr(1, string::npos));
} else {
c = '-';
c += Minus(b.substr(1, string::npos), a);
}
}
return c;
}
int main() {
string a, b, c;
int n;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a >> b >> c;
if (c[0] != '-') {
c = '-' + c;
} else {
c = c.substr(1, string::npos);
}
printf("Case #%d: ", i + 1);
string d = calculate(calculate(a, b), c);
if (d[0] != '-') {
cout << "true" << endl;
} else {
cout << "false" << endl;
}
}
return 0;
}
附柳神代码(简洁高效)
#include <cstdio>
using namespace std;
int main() {
int n;
scanf("%d", &n);
for(int i = 0; i < n; i++) {
long long a, b, c;
scanf("%lld %lld %lld", &a, &b, &c);
long long sum = a + b;
if(a > 0 && b > 0 && sum < 0) {
printf("Case #%d: true\n", i + 1);
} else if(a < 0 && b < 0 && sum >= 0){
printf("Case #%d: false\n", i + 1);
} else if(sum > c) {
printf("Case #%d: true\n", i + 1);
} else {
printf("Case #%d: false\n", i + 1);
}
}
return 0;
}
更多PAT甲级题目:请访问PAT甲级刷题之路--题目索引+知识点分析(正在进行),感谢支持!