Huawei trial-subtraction of large numbers, Huawei large numbers
Question: subtraction of large numbers
Enter a positive integer of two strings. The first line is the subtrahend, the second line is the subtrahend, and the result of the first line minus the second line is output.
Note: 1. Both integers are positive integers. The subtrahend are greater than the subtrahend. Example:
Input: 1000000000000001 1
Output: 1000000000000000
# Include <stdio. h> # include <string. h> # define MAX 100 // subtract int bigNumSub (char a [], char B [], char sub []) {int I = 0; int c = 0; // determine whether the value is enough to reduce, 0 is enough to reduce, and 1 is not enough to reduce // clear 0 char m [MAX] = {0 }; // The subtrahend are greater than the subtrahend of char n [MAX] = {0}; // The subtrahend of memset (sub, 0,100 ); // reverse the string and change the string to the numeric int lenA = strlen (a); int lenB = strlen (B); for (I = 0; I <lenA; I ++) {m [I] = a [lenA-i-1]-'0'; // printf ("% d", m [I]);} for (I = 0; I <lenB; I ++) {n [I] = B [lenB-i-1]-'0'; // printf ("% d", N [I]);} // bitwise operation: the bitwise operation is not enough. The bitwise operation is not enough, and the last bitwise operation is reduced by one for (I = 0; I <lenA; I ++) {c = (m [I]-n [I]) <0? 1:0; sub [I] = m [I] + 10 * c-n [I] + '0 '; // Add '0' to convert if (I + 1 = lenA) break; m [I + 1]-= c;} return lenA ;} void print (char sub [], int lenA) {int I; int k; // the first digit not 0 for (I = lenA-1; I> = 0; I --) {if (sub [I]! = '0') {k = I; break ;}} for (I = k; I> = 0; I --) {printf ("% c ", sub [I]) ;}} int main () {char a [MAX]; char B [MAX]; char sub [MAX]; puts ("input :"); gets (a); puts ("input B:"); gets (B); print (sub, bigNumSub (a, B, sub )); printf ("\ n"); return 0 ;}
Test results may be incomplete. Check for missing items: