2014-12-26
Large-number multiplication algorithm is generally used to simulate the "multiplication calculus process of pupils" method.
Main algorithm idea:
1. Multiply the multiplier a (i) bit with the multiplier B J) and place the product result in the (i+j-1) bit of product result array product;
2. Check whether the number stored in the (i+j-1) bit of product is greater than or equal to 10, and if so, "take out and carry".
Detail Reference Code:
1#include <iostream>2#include <cstring>3 using namespacestd;4 voidSolvestringAstringb) {5 intA_length=a.length ();6 intB_length=b.length ();7 int*arr_a=New int[a_length+1];8 int*arr_b=New int[b_length+1]; 9 Ten for(intI=1; i<=a_length;i++){ OneArr_a[i] = a[i-1]-'0'; A } - for(intI=1; i<=b_length;i++){ -Arr_b[i] = b[i-1]-'0'; the } - - int*product=New int[a_length+B_length]; - intProduct_length = a_length+b_length-1; + for(intI=0; i<=product_length;i++) product[i]=0; - + for(inti=a_length;i>=1; i--){ A at for(intj=b_length;j>=1; j--){ - inttemp = arr_a[i]*Arr_b[j]; - intc = product[i+j-1]+temp; -product[i+j-1] = c%Ten; -product[i+j-2] + = c/Ten; - } in } - to + if(product[0]!=0) cout<<product[0]; - for(intI=1; i<=product_length;i++){ thecout<<Product[i]; *}cout<<Endl; $ }Panax Notoginseng intMain () { - stringb; the while(cin>>a>>b) { + Solve (A, b); A } the return 0; +}View Code
POJ2389: Large number multiplication algorithm