Large integer multiplication (simple analog multiplication process)

Source: Internet
Author: User
Tags encode string

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 <&lt ; 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)

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.