1065 A + B and C (64bit) (20 minutes) / the overflow feature data

topic

Given three integers A, B and C in [−263,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, 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


Variable overflow properties

Because not majors in college, never learned basic courses Principles of Computer Organization and so on, do not understand the variable storage mechanism. Conventional methods can only think of computing large numbers, that is, the method "algorithm notes" in this article.
But think this is a problem in that way I felt quite troublesome, Liu went to see God's blog, found that the use of variable overflow features. So to learn some knowledge of computer memory - " the original code, complement ."

Similarities and differences

They are the first one as a sign bit, 0 for positive and 1 indicates negative.
The original value of the bit is binary code decimal representation.
In computer systems, a positive integer's complement binary representation which is the same as the original code; negated complement integers, which in addition to the original code symbol bits of all the bits (1, 1 becomes 0 becomes 0, the symbol after the bit is 1 unchanged) plus 1.

purpose

Computer Numerical complements to store all is done to facilitate the calculation, because the sign bit of the original code can lead to errors calculated directly. Such as: 1 (1000 ... 001) +1 (001 ... 0000) = -2 (1000 ... 010), is obviously wrong.

Look at the data overflow

FIG complement is defined under the formula, based on the number of axes along lists typical true value, the original code and the two's complement representation. Negative complement representation range than the original code slightly wider, one more digital combination, because in complement representation, only one represents a number of 0 . Thus, when n is an integer representing the range (-2 n ~ 2 n -1). Here Insert Picture Description
This chart will explain the characteristics of the data overflow. Such as int type, a maximum of 2 31 is -1, which is the complement of 0111 ... 11, plus an overflowed, into 1000 ... 00 complement, indicates -2 31 is .

Return to this topic

If A> 0, B <0, or A <0, B> 0, A + B is impossible to overflow
if A> 0, B> 0, sum may overflow, sum should range is (0, 2 64 - 2 ], the results obtained should be the overflow [-2 64 , -2] is a negative number, the a + B <0 when the overflow explained.
Likewise a <0, B <a + B> = 0 0 Description overflow. Note when A = B = -2 63 is time, A + B = 0.

problem

I wrote the following code but there are two test points can not pass.

#include<cstdio>
using namespace std;
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);
		printf("Case #%d: ", i);
		if (a > 0 && b > 0 && a+b < 0) printf("true\n");
		else if (a < 0 && b < 0 && a+b >= 0) printf("false\n");
		else printf("%s\n", a+b > c ? "true" : "false");
	}
	return 0;
}

Here Insert Picture Description
Then we see other people's code is another wrote a long long variable to store a + b, so he AC, but do not understand why this is ...

#include<cstdio>
using namespace std;
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);
		printf("Case #%d: ", i);
		long long sum = a + b;
		if (a > 0 && b > 0 && sum < 0) printf("true\n");
		else if (a < 0 && b < 0 && sum >= 0) printf("false\n");
		else printf("%s\n", sum > c ? "true" : "false");
	}
	return 0;
}

long double Solution

But there are bloggers believe that the above approach is not correct, because the maximum value of A given subject, B, C has exceeded the scope of a long long, long long 2 ^ 63 is unexplained correctly, be misinterpreted as - 2 ^ 63. So even if you can pass the test point, it is still not the right approach.
Input example:
0 1 determines 9223372036854775808 A + B <C; obviously incorrect. But it may be due to the test data and no such data is present, and therefore the AC.
Using a larger storage type long double, it has a higher precision and indicates a range.
NOTE: When the input-output, using the format characters% llf.

double and long double are floating point ANSI C standard. However, ANSI C does not provide the exact precision of long double. They may have different implementations for different platforms. Some 8 bytes, some 10 bytes, 12 bytes or some more. Generally the long double precision than double, at least equal, like int and long int the same. However, the same platform may not be the same.

double precision type is 15 to 16, long double precision depends on the actual compilation environment or computer system used.
Therefore, this solution may not have universality.

#include <cstdio>
int main()
{
    int n;
    scanf("%d", &n);
    for( int i = 1; i <= n; ++i )
    {
        long double A, B, C;
        scanf("%llf %llf %llf", &A, &B, &C);
        printf("Case #%d: %s\n", i, A + B > C ? "true":"false");
    }
}
Published 33 original articles · won praise 5 · Views 2272

Guess you like

Origin blog.csdn.net/weixin_43590232/article/details/104100372