Question: enter two integers a, B,-10100 <a, B <10100, and calculate a-B by programming.

This is a very simple question, because a and B may be positive or negative, so a-B, a-(-B),-a-B,

-A-(-B. There is a-B after parentheses,

-(A-B), a + B,-(a + B. Writing code is still annoying. If we think of binary complement operations, we can convert subtraction into addition to unify addition and subtraction operations, so we can consider implementing decimal complement operations. Decimal complement operations and various hexadecimal operations (when doing this question, I checked the second volume of TAOCP and learned something about arbitrary hexadecimal, including B-hexadecimal and bk-hexadecimal conversions, especially in-10 hexadecimal mode and sqrt (2) hexadecimal mode. It is very interesting to note that there are a lot of materials on the Internet. Here, I just want to write down the process of doing this by myself and forget it.

Binary complement operations are very familiar. (X + Y) Fill = X fill + Y fill, (X-Y) Fill = X fill + (-Y) fill, This is the complement code can be unified addition and subtraction formula, the same is true for decimal. There is a formula for any hexadecimal complement:

[X] fill = M + X (mod m)

With this, it is very easy to apply the complement code to decimal. At that time, I was a bit paranoid. I must find two numbers on the symbol bit to replace the negative effect of 0 and 1 in binary. In binary, 0 indicates nothing, and 1 indicates that if another one is put, it will overflow. Therefore, in decimal, 0 is used to replace 0 in binary, And it is placed in the symbol bit to represent positive, replace 1 in binary with 9 and put it in the symbol bit to indicate negative (in fact, you have complicated the problem, so you don't have to do this ). In this way, the value range of the n decimal BITs, including the symbol bit, can be [-10n-1, 10n-1-1], with a total of 10 n numbers. For example, if the formula-946-37 is calculated: the original code of-46 is 954, the complement code is bitwise, And the last digit is added with 1. The result is 937, the original code of-37 is 963, And the complement code is, complement plus (-46) Complement + (-37) Complement = 954 + 963 = 1917, the symbol carries out, the result is 917,917 followed by bitwise decimal last bit plus 1 to get the original code 983, which indicates the true value is-83, so there is-46-37 =-83.

If you want to calculate-99-99, it will overflow, so it must be expressed in four decimal places. But one byte is a cool for the computer, so the program is written here. The Code is as follows:

# Include <stdio. h> <br/> # include <string. h> <br/> char a [128], B [128], TA [128], TB [128]; <br/> int main () {<br/> int I, j; <br/> scanf ("% S % s", a, B ); <br/> If (* A = '-') {<br/> Ta [103] = 9; <br/> for (I = 0, j = strlen (a)-1; j> = 1; I ++, j --) <br/> Ta [I] = 9-(A [J]-'0'); <br/> while (I <= 102) {ta [I] = 9; I ++ ;}< br/> Ta [0] + = 1; <br/>} else {<br/> Ta [103] = 0; <br/> for (I = 0, j = strlen (a)-1; j> = 0; I ++, j --) <br/> Ta [I] = A [J]-'0 '; <br/>}< br/> If (* B = '-') {<br/> TB [103] = 0; <br/> for (I = 0, j = strlen (B)-1; j> = 1; I ++, j --) <br/> TB [I] = B [J]-'0'; <br/>}else {<br/> TB [103] = 9; <br/> for (I = 0, j = strlen (B)-1; j> = 0; I ++, j --) <br/> TB [I] = 9-(B [J]-'0'); <br/> while (I <= 102) {TB [I] = 9; I ++ ;}< br/> TB [0] + = 1; <br/>}< br/> for (I = 0; I <= 103; I ++) {<br/> TB [I] + = TA [I]; <br/> TB [I + 1] + = Tb [I]/10; <br/> TB [I] % = 10; <br/>}< br/> // print <br/> If (Tb [103] = 0) {<br/> for (I = 102; TB [I] = 0; I --); <br/> while (I> = 0) {putchar (Tb [I] + '0'); I --;} <br/> putchar ('/N'); <br/>}else {<br/> for (I = 0; I <= 102; I ++) <br/> TB [I] = 9-TB [I]; <br/> TB [0] + = 1; <br/> for (I = 0; TB [I]> = 10; I ++) {<br/> TB [I + 1] + = Tb [I]/10; <br/> TB [I] % = 10; <br/>}< br/> putchar ('-'); <br/> for (I = 102; TB [I] = 0; I --); <br/> while (I> = 0) {putchar (Tb [I] + '0'); I --;} <br/> putchar ('/N'); <br/>}< br/> return 0; <br/>}

This code is only for AC.

Now let's make a summary. The binary complement definition is very beautiful because it has symmetry. In binary, many things are "exactly like this ". It happens that the binary is only 0 and 1, and there are only positive and negative States, that is, 2n + 2n = 2n + 1 ....... Under the corresponding rules of the complement code, there are still so many numbers that the number of n bits can represent, but the positive number of half of them is given to a negative number. In this sense, let's look at the decimal complement. If it is a n-digit decimal number, it can represent a 10 n decimal number string. If it uses a complement code, it must be divided into half to represent a negative number, therefore, it can only represent 0.5 * 10n = 5*10n-1 positive numbers. The other half is negative. We often say that the advantage of "symbol bit involved in Operation" in the complement representation is actually nothing, except that the digits with the highest weight can represent both positive and negative numbers, it should and must be involved in the operation.

The format of the binary complement is defined as follows: for a fixed point integer n + 1 decimal point integer X = XnXn-1... X0, whose complement code is:

[X] fill = X, if 0 <= X <=

5*10n-1,

[X] fill = 10n + 1 + X = 10n + 1-| X | if-5 * 10n <= X

<=-1

When the maximum value of Xn is, 4, it indicates positive. If it is, 9, it indicates negative. For example, the three-digit decimal number can represent-500 to 499. For example, the following calculation:

-327 + 164:-327 is 673,-327, + 673,

-327 + 164*673 + 164 = 837,837 is a negative number, and it is in the form of a complement Code. Its absolute value is 163. Therefore,-327 + 164 =-163.

Overflow occurs when the absolute value of the calculation result is greater than 500. You can use dual-sign bit detection in the computer composition principle to detect whether it is overflow or underflow, but it is not necessary to use the extra sign bit for computing, so that the range of numbers that can be expressed is increased by ten times, and the previous overflow will naturally disappear.

The following are other interesting things I learned when I read this section of the second section of knuth's taocp. I just took a look at it and noted it down here: 1. The conversion between B and BK is very simple, see the conversion between binary and octal and hexadecimal. 2. The-10 and-2 hexadecimal formats are very interesting. -The binary system seems to have been applied. Others, such as SQRT (2) I hexadecimal, are amazing. These hexadecimal definitions are the same as those of other hexadecimal systems, except that the weights of each digit are different. For example, if you want to convert the decimal number 126 to-10, you can use the remainder method except the base. The decimal number is 126/(-10) =-12 ...... 6,

-12/(-10) = 2 ...... 8, so the decimal 126 in-10 represents 286. Check that 2 * (-10) 2 + 8 *

(-10) 1 + 6 = 126. However, the calculation rules of the-10 hexadecimal number have not been learned yet.

You can find proof on the Internet about how the original code is converted into a supplementary code.