高精度求大数乘法

 1 #include<iostream>
 2 #include<cstring>
 3 #include<string>
 4 using namespace std;
 5 int main()
 6 {
 7     char str1[256],str2[256];
 8     int a[256],b[256],c[256];
 9     int lena,lenb,lenc;
10     int x;
11     int i,j;
12 
13     memset(a,0,sizeof(a));
14     memset(b,0,sizeof(b));
15     memset(c,0,sizeof(c));
16 
17     cin>>str1;//输入乘数str1
18     cin>>str2;//输入乘数str2
19 
20     lena=strlen(str1);
21     lenb=strlen(str2);
22     for(i=0;i<=lena-1;i++)//乘数str1存入数组a
23         a[lena-i]=str1[i]-'0';
24     for(i=0;i<=lenb-1;i++)//乘数str2存入数组b
25         b[lenb-i]=str2[i]-'0';
26 
27     for(i=1;i<=lenb;i++)
28     {
29         x=0;//用于存放进位
30         for(j=1;j<=lena;j++)//对乘数每一位进行处理
31         {
32             c[i+j-1]=a[j]*b[i]+x+c[i+j-1];//当前乘积+上次乘积进位+原数
33             x=c[i+j-1]/10;
34             c[i+j-1]%=10;
35         }
36         c[i+lena]=x;//进位
37     }
38     lenc=lena+lenb;
39     while((c[lenc]==0)&&(lenc>1))//删除前导0
40         lenc--;
41     for(i=lenc;i>=1;i--)//倒序输出
42         cout<<c[i];
43     cout<<endl;
44     return 0;
45 }
原文地址:https://www.cnblogs.com/--lr/p/12837324.html