10106 - Product

 

 

 Product 

 

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
 






高精度乘法,注意进位的时候,遍历K次,就要从第K位开始加起
由于只进行了len次循环,最后一位可能还可以进位,所以要加一个判断,使得最后一位不会丢失

#include <cstdio>
#include <cstring>
char x[500];
char y[500];
int s1[500],s2[500];
int s[1000];
int main()
{
    int lenx,leny;
    while (scanf("%s%s",x,y)!=EOF)
    {
        memset(s1,0,sizeof(s1));
        memset(s2,0,sizeof(s2));
        memset(s,0,sizeof(s));
        lenx=strlen(x);
        leny=strlen(y);
        for (int i=lenx-1,j=0; i>=0; i--,j++)
        {
            s1[j]=(int)x[i]-'0';
        }
        for (int ii=leny-1,jj=0; ii>=0; ii--,jj++)
        {
            s2[jj]=(int)y[ii]-'0';
        }
        int q,k;
        for (k=0; k<lenx; k++)
        {
            int c=0,temp;
            for (q=0; q<leny; q++)
            {
                temp=s[k+q]+s1[k]*s2[q]+c;
                s[k+q]=temp%10;
                c=temp/10;
            }
            if (c>0) s[q+k]=c;
        }
        int p;
        for (p=999; p&&!s[p]; p--);
        p++;
        while (p--)
        {
         printf("%d",s[p]);
        }
        putchar('\n');
    }
    return 0;
}





posted @ 2013-01-20 18:12  KRisen  阅读(166)  评论(0编辑  收藏  举报