Product

题目如下:

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;
}

    
    
   
   

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值