UVA10106 Product

高精度乘法

题目:

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

解答:

 

 1 #include<stdio.h>
 2 #include<string.h>
 3 struct bign
 4 {
 5     int len;
 6     int s[1000];
 7 };
 8 bign a,b,res;
 9 void change(char *str,bign *b)
10 {
11     int i;
12     b->len=strlen(str);
13     for(i=0;i<b->len;i++)
14         b->s[i]=str[b->len-i-1]-'0';
15 }
16 void mult(bign *a,bign *b,bign *res)
17 {
18     res->len=a->len+b->len;
19     int i,j;
20     for(i=0;i<a->len;i++)
21     {
22         for(j=0;j<b->len;j++)
23         {
24             res->s[i+j]+=a->s[i]*b->s[j];
25         }
26     }
27     for(i=0;i<res->len-1;i++)
28     {
29         res->s[i+1]+=res->s[i]/10;
30         res->s[i]%=10;
31     }
32     while(res->len>1&&res->s[res->len-1]==0)
33         res->len--;
34 }
35 char s1[1000],s2[1000];
36 int main()
37 {
38     while(scanf("%s%s",s1,s2)!=EOF)
39     {
40         memset(res.s,0,sizeof(res.s));
41         change(s1,&a);
42         change(s2,&b);
43         mult(&a,&b,&res);
44         int i;
45         for(i=res.len-1;i>=0;i--)
46             printf("%d",res.s[i]);
47         printf("\n");
48     }
49     return 0;
50 }

 

 

 

posted @ 2013-01-29 15:34  上白泽慧音  阅读(364)  评论(0编辑  收藏  举报