Algorithm question-multiplication of large numbers
Today, I saw a question about how to multiply a large number on the Internet. The question is: enter two integers and output the product of these two numbers. The input number may exceed the storage range of the computer's integer data.
Analysis:
Since numbers cannot be stored with an integer variable, it is natural to use strings to represent a string of numbers. Then, according to the multiplication rule, multiply each bit of a multiplier by another multiplier, and then add all intermediate results to the correct position to obtain the final result. It can be analyzed that if the multiplier is a and B, the number of digits of a is m, and the number of digits of B is N, the product result is m + N-1 (the highest bit has no carry) or m + N bits (the highest bit has an inner ). Therefore, an M + N can be allocated to store the final result. To save space, all intermediate results are accumulated directly on the m + n secondary storage. Finally, in order to better comply with our multiplication logic, we can talk about the number reverse storage, so that the low position of the number is at the low subscript position of the array, it is easier to determine the subscript position when accumulating.
The following is my solution.
The first is the reverse function of the array:
Press Ctrl + C to copy the code
Void reverseorder (char * STR, int P, int q)
{
Char temp;
While (P <q)
{
Temp = STR [p];
STR [p] = STR [Q];
STR [Q] = temp;
P ++;
Q --;
}
}Press Ctrl + C to copy the code
Then, the function that completes the multiplication of large numbers:
1 char * multilargenum (char * a, char * B) 2 {3 int M = strlen (a); 4 int n = strlen (B ); 5 char * result = new char [M + n + 1]; 6 memset (result, '0', m + n ); 7 result [M + N] = '\ 0'; 8 reverseorder (A, 0 m-1); 9 reverseorder (B, 0, n-1); 10 11 int multiflag; // product carry 12 INT addflag; // addition carry 13 for (INT I = 0; I <= n-1; I ++) // each digit of B 14 {15 multiflag = 0; 16 addflag = 0; 17 for (Int J = 0; j <= m-1; j ++) // each digit of A is 18 {19 // '0'-48 = 020 int temp1 = (a [J]-48) * (B [I]-48) + multiflag; 21 multiflag = temp1/10; 22 temp1 = temp1 % 10; 23 int temp2 = (result [I + J]-48) + temp1 + addflag; 24 addflag = temp2/10; 25 result [I + J] = temp2 % 10 + 48; 26} 27 result [I + M] + = multiflag + addflag; 28} 29 reverseorder (result, 0, M + N-1); // returns 30 31 return result in reverse order; 32}
Finally, the test program:
1 int main() 2 { 3 char A[] = "962346239843253528686293234124"; 4 char B[] = "93459382645998213649236498"; 5 char *res = multiLargeNum(A, B); 6 if(res[0] != 48) 7 printf("%c", res[0]); 8 printf("%s", res+1); 9 delete [] res;10 return 0;11 }
Time Complexity Analysis:
The time for the three reverse operations is O (n), O (M), O (m + n), and the time complexity of the Dual Loop is O (Mn ), the total time complexity is O (Mn + (m + n). Generally, m + n <Mn, so it can be considered as O (Mn ). In addition, reverse operations are only intended for easier thinking and can be completely removed.
The space complexity is O (m + n)