高精度 乘

 #include <bits/stdc++.h>
 
 using namespace std;
 
 const int N = 1e6+10;
 
 int n,m, a[N],b[N],c[N] , la , lb , lc;
 string s1,s2;
 //  i.e. 12 * 96  
 //      a[1] : 2 a[2] : 1 
 //      b[1] : 6 b[2] : 9   
 //    						  2*6
 // 1nd round : c[1] ~ c[4] :  2 1 0 0
 //                            
 // 2nd round : c[1] ~ c[4] :  2 9 1 0
 //                             +6
 // 3nd round : c[1] ~ c[4] :  2 5 2 0 
 // 4nd round : c[1] ~ c[4] :  1 1 5 2  
 //           final results : 2511
 
 int main()
 {
 	cin >> s1 >> s2;
 	la = s1.size(), lb = s2.size();
 	for(int i = 0 ; i < la ; i++)	a[la - i] = s1[i] - 48;
 	for(int i = 0 ; i < lb ; i++)	b[lb - i] = s2[i] - 48;
 	
 	lc = la + lb;
 	for(int i = 1 ; i <=  la; i++)  
 	{
 		for(int j =  1; j <= lb ; j++)
		{
			c[i+j-1] += a[i] * b[j];
			c[i+j] += c[i+j-1] /10;
			c[i + j -1] %= 10;
//				for(int i = lc ; i >= 1 ; i--)cout << c[i] ;	cout << endl;
		} 	

	 }		
		
	while(lc > 1 && c[lc] == 0) lc --;
	
	for(int i = lc;  i >= 1 ; i--)	cout << c[i];
	
	return 0;
 }
posted @ 2022-03-12 00:02  HarySeldon  阅读(30)  评论(0编辑  收藏  举报