10106 - Product

来源:互联网 发布:python输入ctrl c 编辑:程序博客网 时间:2024/06/17 13:28
 Product 

TheProblem

The problem is to multiply two integers X, Y.(0<=X,Y<10250)

TheInput

The input will consist of a set of pairs of lines. Each line inpair contains one multiplyer.

TheOutput

For each input pair of lines the output line should consist oneinteger the product.

SampleInput

12122222222222222222222222222

SampleOutput

144444444444444444444444444
 
代码:
#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;
}
原创粉丝点击