|
The Problem
The problem is to multiply two integers X, Y. (0<=X,Y<10250)
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
代码:
#include<stdio.h>
#include<string.h>
#include<ctype.h>
int len1,len2,z,ans[1001],ans1[1001];
void fun(char x[1001],char y[1001])
{
int i,j,k,c,bsum,t;
for(i=0;i<len2;i++)
{
memset(ans1,0,sizeof(ans1));
k=i;
c=bsum=0;
for(j=0;j<len1;j++)
{
bsum=c+(x[j]-'0')*(y[i]-'0');
ans1[j+k]=bsum;
c=bsum/10;
}
if(c>0)
ans1[j+k]=c;
c=bsum=0;
for(t=0;t<1001;t++)
{
bsum=ans[t]+ans1[t]+c;
ans[t]=bsum;
c=bsum/10;
}
}
}
int main()
{
int i,j;
char x[1001],y[1001],x1[1001],y1[1001];
while(scanf("%s%s",x,y)==2)
{
len1=strlen(x);
len2=strlen(y);
for(i=0,j=len1-1;i<len1;i++,j--)
x1[j]=x[i];
for(i=0,j=len2-1;i<len2;i++,j--)
y1[j]=y[i];
memset(ans,0,sizeof(ans));
if(len1>=len2)fun(x1,y1);
else
{
z=len1;
len1=len2;
len2=z;
fun(y1,x1);
}
for(i=1000;i>=0;i--)
if(ans[i]!=0)break;
if(i==-1)printf("0");
for(i;i>=0;i--)
printf("%d",ans[i]);
printf("\n");
}
return 0;
}