题目如下:
The Problem
The problem is to multiply two integers X, Y. (0<=X,Y<10^250)
The Input
The input will consist of a set of pairs of lines. Each line in pair contains one multiplyer.
The Output
For each input pair of lines the output line should consist one integer the product.
Sample Input
12
12
2
222222222222222222222222
Sample Output
144
444444444444444444444444
高精度乘法,和加法思想差不多,因为用memset初始化的时候没用sizeof,而用的magic number,没初始化成功,所以导致WA。。。
AC的代码如下:
#include
#include
char x[505]= {'\0'},y[505]= {'\0'};
int temp[505][600]= {0},ans[505]= {0},x2[505]= {0},y2[505]= {0};
int main()
{
while(scanf("%s%s",x,y)!=EOF)
{
memset(x2,0,sizeof(x2));
memset(y2,0,sizeof(y2));
memset(ans,0,sizeof(ans));
memset(temp,0,sizeof(temp));
int len1,len2,k=0;
len1=strlen(x);
len2=strlen(y);
int i=0,j=0,c=0,ok=0;
for(i=0; i<=len1-1; i++)
x2[i]=x[len1-1-i]-'0';
for(i=len1; i<=504; i++)
x2[i]=0;
for(j=0; j<=len2-1; j++)
y2[j]=y[len2-1-j]-'0';
for(j=len2; j<=504; j++)
y2[j]=0;
for(i=0; i<=504; i++)
{
c=0;
for(j=0,k=i; j<=504; j++,k++)
{
temp[i][k]=(x2[j]*y2[i]+c)%10;
c=(x2[j]*y2[i]+c)/10;
}
}
c=0;
for(j=0; j<=504; j++)
{
for(i=0; i<=504; i++)
ans[j]=ans[j]+temp[i][j];
ans[j]=ans[j]+c;
c=ans[j]/10;
ans[j]=ans[j]%10;
}
for(j=504; j>=0; j--)
{
if(ans[j]!=0)
ok=1;
if(ok==1)
printf("%d",ans[j]);
}
if(ok==0)
printf("%d",0);
printf("\n");
memset(x,'\0',sizeof(x));
memset(y,'\0',sizeof(y));
}
return 0;
}