代码
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
int main ()
{
char a1[101],b1[101];
int a[101],b[101],c[10001],lena,lenb,lenc,i,j,x;
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
memset(c,0,sizeof(c));
scanf("%s",a1);
scanf("%s",b1);
lena=strlen(a1);
lenb=strlen(b1);
for(i=0;i<=lena-1;i++)
{
a[lena-i]=a1[i]-48;
}
for(i=0;i<=lena-1;i++)
{
b[lena-i]=b1[i]-48;
}
for(i=1;i<=lena;i++)
{
x=0;
for(j=1;j<=lenb;j++)
{
c[i+j-1]=a[i]*b[j]+x+c[i+j-1];
x=c[i+j-1]/10;
c[i+j-1]%=10;
}
c[i+lenb]=x;
}
lenc=lena+lenb;
while(c[lenc]==0&&lenc>1)
{
lenc--;
}
for(i=lenc;i>=1;i--)
{
cout<<c[i];
}
cout<<endl;
return 0;
}
升级版代码
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
int a[10000];
string s1,s2;
void addd()
{
int a1=s1.size(),a2=s2.size();
for (int i=0;i<a1;i++)
for (int j=0;j<a2;j++)
a[a1-i+a2-j-1]+=(s1[i]-'0')*(s2[j]-'0');
for (int i=1;i<=a1+a2;i++) { a[i+1]+=a[i]/10; a[i]%=10; }
a[0]=a1+a2; while (a[a[0]]==0&&a[0]>1) a[0]--;
}
int main()
{
getline(cin,s1);
getline(cin,s2);
memset(a,0,sizeof(a));
addd();
for (int i=a[0];i>=1;i--) cout<<a[i];
cout<<endl;
return 0;
}