A large number is a number that cannot be expressed by the Data Type in the algorithm language. The number of digits exceeds the range that can be expressed by the maximum data type. Therefore, when dealing with large numbers, you must first consider how to store large numbers, then the implementation method of its processing in this storage mode.
Generally, a large number is stored in a character array, that is, a large number is stored as a string, and its processing is simulated in the array according to its processing rules.
Subtraction of a large number.
The processing idea of the Large Number subtraction method is similar to the addition method. First, judge the size of a and B, then perform bitwise calculation based on the conditions, and handle the bitwise. In this case, if the value of a digit is smaller than 0, it is used as a forward borrow.
The idea is common, and it is not difficult to directly use the code.
The level is limited. Currently, only such troublesome algorithms can be written. Hope. In the future, we will be able to optimize it when we have time.
# Include <stdio. h> # include <string. h> int compare (char * str_a, char * str_ B) {int len_a, len_ B; len_a = strlen (str_a ); // compare len_ B = strlen (str_ B); if (strcmp (str_a, str_ B) = 0) // return the comparison result return 0; if (len_a> len_ B) return 1; else if (len_a = len_ B) return strcmp (str_a, str_ B); else return-1;} int main () {int f, n; int I, k, len_a, len_ B; char str_a [1000], str_ B [1000]; int num_a [1000] = {0}; // initialize the array of large numbers. All values are 0 int num_ B [1000] = {0}; int num_c [1000]; while (scanf ("% s ", str_a, str_ B )! = EOF) // multiple groups of tests can be performed. {len_a = strlen (str_a); // two digits of the two large numbers are obtained, respectively. len_ B = strlen (str_ B); k = len_a> len_ B? Len_a: len_ B; // obtain the maximum number of digits num_c [0] = 0; f = 0; n = compare (str_a, str_ B); for (I = 0; I <len_a; I ++) // inverted storage num_a [I] = str_a [len_a-i-1]-'0'; for (I = 0; I <len_ B; I ++) num_ B [I] = str_ B [len_b-i-1]-'0'; for (I = 0; I <k; I ++) // perform Subtraction by bit {if (n> = 0) {if (num_a [I]> = num_ B [I]) num_c [I] = num_a [I]-num_ B [I]; else {num_c [I] = num_a [I]-num_ B [I] + 10; num_a [I + 1] -- ;}} else {if (num_ B [I]> = num_a [I]) num_c [I] = num_ B [I]-num_a [I]; else {num_c [I] = num_ B [I]-num_a [I] + 10; num_ B [I + 1] -- ;}}} if (n <0) // print printf ("-") as required; for (I = K-1; I> = 0; I --) {if (num_c [I]) f = 1; if (f | I = 0) printf ("% d", num_c [I]);} printf ("\ n"); for (I = 0; I <k; I ++) // clears 0. perform the next operation {num_a [I] = 0; num_ B [I] = 0 ;}} return 0 ;}