Poj2389-Bull Math (big integer multiplication), poj2389-bullmath
I. Question:
Big integer multiplication template question
Ii. Ideas:
1. Simulate multiplication (note: "Every ten in one ")
2. inverted output (note that the first 0 is not output)
Step 3:
For example, 555x35 = 19425
5 5 5 5 5 5
X 3 5x3 5
----------- ==> ----------
2 7 7 5 25 25
+ 1 6 6 5 + 15 15 15
------------------------------
1 9 4 2 5 15 40 40 25
Ten in one
---------------
1 9 4 2 5
1 # include <iostream> 2 # include <cstring> 3 using namespace std; 4 const int N = 10010; 5 char a [N], B [N]; 6 int ans [N]; // record result array 7 int digit; // record result number 8 9 // simulate multiplication process 10 void MUL (int len, int len2) {11 int l = 0; 12 for (int I = len2-1; I> = 0; I --) {13 int k = l; 14 int m = l; 15 for (int j = len-1; j> = 0; j --) {16 ans [k ++] = (B [I]-'0') * (a [j]-'0') + ans [m ++]; // The storage time is 17} 18 l ++ in reverse order; // after one digit, move one digit back Add 19 digit = k; // record digits 20} 21 // perform the "every ten into one" Operation 22 for (int I = 0; I <= digit; I ++) {23 if (ans [I]> = 10) {24 ans [I + 1] = ans [I + 1] + ans [I]/10; 25 ans [I] % = 10; 26} 27} 28} 29 30 // output operation 31 void print () {32 if (ans [digit]! = 0) // determine whether the first digit is 0 33 cout <ans [digit]; 34 for (int I = digit-1; I> = 0; I --) {// output 35 cout in reverse order <ans [I]; 36} 37 cout <endl; 38} 39 40 int main () {41 while (cin> a> B) {42 int len = strlen (a); 43 int len2 = strlen (B); 44 memset (ans, 0, sizeof (ans); // initialize ans [] to 0 45 MUL (len, len2); 46 print (); 47} 48 return 0; 49}View Code
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.