Increase or subtract 1 from a large number -- put both numbers before and after the inversion to align the digit

Source: Internet
Author: User
# Include <stdio. h> // store the read data to num [1] ~ In num [X], num [0] indicates the length of the stored data. Void read (INT num []) {int I; char ch; for (I = 0; I <500; I ++) num [I] = 0; I = 1; while (CH = getchar ())! = '\ N') {num [I] = CH-'0'; I ++; num [0] ++ ;}} // flip the number in data num [] to calculate void Rev (INT num []) {int I, T; for (I = 1; I <= num [0]/2; I ++) {T = num [I]; num [I] = num [num [0] + 1-I]; num [num [0] + 1-I] = T;} void print (INT num []) {char character [11] = {"0123456789 -"}; // character [N] indicates the character form of number n. Character [10] = '-' is used to output the negative number in the subtraction result. Int I; for (I = 1; I <= num [0]; I ++) printf ("% C", character [num [I]); printf ("\ n");} // Add the values of a [] and B [] and store them in sum. Void add (INT sum [], int A [], int B []) {// flag is the carry flag. Int I, flag = 0; Rev (a); Rev (B); for (I = 1; I <= A [0] | I <= B [0]; I ++) sum [I] = A [I] + B [I]; if (a [0]> B [0]) sum [0] = A [0]; else sum [0] = B [0]; for (I = 1; I <= sum [0]; I ++) {sum [I] + = flag; If (sum [I]> 9) {sum [I] = sum [I] % 10; flag = 1 ;} else flag = 0;} if (1 = Flag) {sum [0] + = 1; sum [I] = 1;} Rev (SUM); Rev (); // After the addition operation, flip a and B back. Rev (B);} void sub (INT rst [], int A [], int B []) {int I; int flag = 0; // flag is a flag. Rev (a); Rev (B); for (I = 1; I <= A [0] | I <= B [0]; I ++) RST [I] = A [I]-B [I]; if (a [0]> B [0]) rst [0] = A [0]; else rst [0] = B [0]; for (I = 1; I <= rst [0]; I ++) {rst [I] + = flag; if (RST [I] <0) {flag =-1; rst [I] + = 10;} else flag = 0;} If (-1 = Flag) {// at this time, flag =-1 indicates a-B <0, so it is converted to B-. You need to first flip A and B to the original data Rev (a); Rev (B); sub (RST, B, A); // After the sub operation, A, B, the RST is in the normal format. You need to flip it and add a negative number. Rev (RST); Rev (a); Rev (B); // Add the '-' rst [0] ++ to the highest bit of the result; RST [rst [0] = 10; // If a value is 10, this bit is "-";} Rev (RST); Rev (); rev (B);} int main () {int A [500], B [500], he [500], Cha [500]; printf ("input: "); read (A); printf (" input B: "); read (B); add (HE, a, B); sub (CHA, A, B ); print (HE); print (CHA); Return 0 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.