First, analysis
When the number of integers exceeds the maximum value that the computer hardware can represent, then we can only use the software method to achieve multiplication of large integers.
We can use string to simulate the multiplication of large integers, the idea of the algorithm is to use the multiplication we learned in primary school, a phase multiplication, and finally calculate the result. As follows:
1 2 3
x 1 2
------------------------
2 46
1 2 3
------------------------
1 4 7 6
To simulate the multiplication process, we need to use two string variables, one to save the product result for each step, and the other to save the final result.
Time complexity: The algorithm time is always consumed in two layers of cycle, so the time complexity is O (n^2).
Second, the Code implementation
#include <iostream> #include <string> #include <vector>using namespace std;//the product of two large numbers (two numbers are positive) string Getproductoftwobignum (String strnumleft, String strnumright) {//////////////////////////////////////uses the methods learned in primary school, Calculates two large number products///////////////////////////////////if (strnumright.empty () && strnumright.empty ()) {return string (" 0 ");} Convert to Digital for (string::size_type i = 0; i < strnumleft.size (); ++i) {strnumleft[i]-= ' 0 ';} for (String::size_type i = 0; i < strnumright.size (); ++i) {strnumright[i]-= ' 0 ';} String::size_type nmaxbits = strnumleft.size () + strnumright.size () + 1;//maximum number of bits, one more bit, easy to encode string strproduct (Nmaxbits, NULL);//save each step by accumulating plus and char sztemp = null;//per bit product, auxiliary variable char szcarraytemp = null;//carry information for (int i = Strnumright.size ()-1; ; = 0; -i) {string Strproductstep (nmaxbits, NULL);//save each step and int k = Strnumright.size ()-i-1;for (int j = strnumleft.size ()- 1; J >= 0; --J) {sztemp = (strnumright[i] * Strnumleft[j] + strproductstep[k])% 10;szcarraytemp = (stRnumright[i] * Strnumleft[j] + strproductstep[k])/10;strproductstep[k] = sztemp;strproductstep[++k] + = szCarrayTemp;} Add this step result in strproduct for (string::size_type m = 0; m < nMaxBits-1; ++m) {sztemp = (Strproductstep[m] + strproduct[m) )% 10;szcarraytemp = (Strproductstep[m] + strproduct[m])/10;strproduct[m] = sztemp;strproduct[m+1] + = szCarrayTemp;} }//returns a traversal strproduct, which takes out the result of the calculation string Strresult;int k = nmaxbits-1;while (k >= 0 && strproduct[k] = = NULL) {--k;} for (; k >= 0;--k) {Strresult.push_back (strproduct[k] + ' 0 ');//Convert to character}if (Strresult.empty ()) {Strresult.push_back (' 0 ' );} return strresult;} int main () {string strnumleft;string strnumright; cout << "Enter two multipliers:"; while (Cin >> Strnumleft >> Strnumri ght) {String strresult = Getproductoftwobignum (Strnumleft, strnumright); cout << "Two-number product:" << strresult << ; Endl;cout << "-------------------------------------------------" << endl;cout << "Enter two multipliers:";} return 0;}
Iii. Results of operation
Hill son
Reprint Please indicate the source, thank you. Original address:http://blog.csdn.net/s634772208/article/details/46505949
Large integer multiplication (simple analog multiplication process)