Copy codeThe Code is as follows: # include <stdio. h>
# Include <string. h>
# Include <stdlib. h>
# Include <math. h>
# Include <assert. h>
# Include <ctype. h>
# Include <map>
# Include <string>
# Include <set>
# Include <bitset>
# Include <utility>
# Include <algorithm>
# Include <vector>
# Include <stack>
# Include <queue>
# Include <iostream>
# Include <fstream>
# Include <list>
Using namespace std;
Const int MAXL = 500;
Struct BigNum
{
Int num [MAXL];
Int len;
};
// Compare a> B return 1, a = B return 0; a <B return-1;
Int Comp (BigNum & a, BigNum & B)
{
Int I;
If (a. len! = B. len) return (a. len> B. len )? 1:-1;
For (I = a. len-1; I> = 0; I --)
If (a. num [I]! = B. num [I]) return (a. num [I]> B. num [I])? 1:-1;
Return 0;
}
// High-precision Addition
BigNum Add (BigNum & a, BigNum & B)
{
BigNum c;
Int I, len;
Len = (a. len> B. len )? A. len: B. len;
Memset (c. num, 0, sizeof (c. num ));
For (I = 0; I <len; I ++)
{
C. num [I] + = (a. num [I] + B. num [I]);
If (c. num [I]> = 10)
{
C. num [I + 1] ++;
C. num [I]-= 10;
}
}
If (c. num [len])
Len ++;
C. len = len;
Return c;
}
// High-precision subtraction to ensure a> = B
BigNum Sub (BigNum & a, BigNum & B)
{
BigNum c;
Int I, len;
Len = (a. len> B. len )? A. len: B. len;
Memset (c. num, 0, sizeof (c. num ));
For (I = 0; I <len; I ++)
{
C. num [I] + = (a. num [I]-B. num [I]);
If (c. num [I] <0)
{
C. num [I] + = 10;
C. num [I + 1] --;
}
}
While (c. num [len] = 0 & len> 1)
Len --;
C. len = len;
Return c;
}
// High Precision multiplied by low precision. When B is large, the int range may overflow. Specific analysis
// If B is large, consider B as high precision.
BigNum Mul1 (BigNum & a, int & B)
{
BigNum c;
Int I, len;
Len = a. len;
Memset (c. num, 0, sizeof (c. num ));
// Multiply by 0 and return 0 directly
If (B = 0)
{
C. len = 1;
Return c;
}
For (I = 0; I <len; I ++)
{
C. num [I] + = (a. num [I] * B );
If (c. num [I]> = 10)
{
C. num [I + 1] = c. num [I]/10;
C. num [I] % = 10;
}
}
While (c. num [len]> 0)
{
C. num [len + 1] = c. num [len]/10;
C. num [len ++] % = 10;
}
C. len = len;
Return c;
}
// Multiply the high precision by the high precision, and pay attention to carry in time; otherwise, overflow may occur, but this will increase the complexity of the algorithm,
// If it is determined that no overflow will occur, you can change the while in it to if
BigNum Mul2 (BigNum & a, BigNum & B)
{
Int I, j, len = 0;
BigNum c;
Memset (c. num, 0, sizeof (c. num ));
For (I = 0; I <a. len; I ++)
{
For (j = 0; j <B. len; j ++)
{
C. num [I + j] + = (a. num [I] * B. num [j]);
If (c. num [I + j]> = 10)
{
C. num [I + j + 1] + = c. num [I + j]/10;
C. num [I + j] % = 10;
}
}
}
Len = a. len + B. len-1;
While (c. num [len-1] = 0 & len> 1)
Len --;
If (c. num [len])
Len ++;
C. len = len;
Return c;
}
// High Precision divided by low precision. The division result is c, and the remainder is f.
Void Div1 (BigNum & a, int & B, BigNum & c, int & f)
{
Int I, len = a. len;
Memset (c. num, 0, sizeof (c. num ));
F = 0;
For (I = a. len-1; I> = 0; I --)
{
F = f * 10 + a. num [I];
C. num [I] = f/B;
F % = B;
}
While (len> 1 & c. num [len-1] = 0)
Len --;
C. len = len;
}
// High precision * 10
Void Mul10 (BigNum &)
{
Int I, len = a. len;
For (I = len; I> = 1; I --)
A. num [I] = a. num [I-1];
A. num [I] = 0;
Len ++;
// If a = 0
While (len> 1 & a. num [len-1] = 0)
Len --;
}
// Divide the precision by the high precision. The division result is c and the remainder is f.
Void Div2 (BigNum & a, BigNum & B, BigNum & c, BigNum & f)
{
Int I, len = a. len;
Memset (c. num, 0, sizeof (c. num ));
Memset (f. num, 0, sizeof (f. num ));
F. len = 1;
For (I = len-1; I> = 0; I --)
{
Mul10 (f );
// The remainder is multiplied by 10 each time.
F. num [0] = a. num [I];
// Add the remainder to the next Digit
/// Use subtraction to replace division
While (Comp (f, B)> = 0)
{
F = Sub (f, B );
C. num [I] ++;
}
}
While (len> 1 & c. num [len-1] = 0)
Len --;
C. len = len;
}
Void print (BigNum & a) // output large number
{
Int I;
For (I = a. len-1; I> = 0; I --)
Printf ("% d", a. num [I]);
Puts ("");
}
// Convert the string to a large number, which exists in the BigNum struct.
BigNum ToNum (char * s)
{
Int I, j;
BigNum;
A. len = strlen (s );
For (I = 0, j = a. len-1; s [I]! = '\ 0'; I ++, j --)
A. num [I] = s [j]-'0 ';
Return;
}
Void Init (BigNum & a, char * s, int & tag) // convert a string to a large number
{
Int I = 0, j = strlen (s );
If (s [0] = '-')
{
J --;
I ++;
Tag * =-1;
}
A. len = j;
For (; s [I]! = '\ 0'; I ++, j --)
A. num[ J-1] = s [I]-'0 ';
}
Int main (void)
{
BigNum a, B;
Char s1 [100], s2 [100];
While (scanf ("% s", s1, s2 )! = EOF)
{
Int tag = 1;
Init (a, s1, tag); // converts a string to a large number.
Init (B, s2, tag );
A = Mul2 (a, B );
If (a. len = 1 & a. num [0] = 0)
{
Puts ("0 ");
}
Else
{
If (tag <0) putchar ('-');
Print ();
}
}
Return 0;
}