zrq495
www.zrq495.com

大数乘法。

代码如下:

 1 #include<stdio.h>
 2 #include<string.h>
 3 
 4 void func(char a[])
 5 {
 6     int i, len=strlen(a);
 7     char t;
 8     for (i=0; i<len/2; i++)
 9     {
10         t=a[i];
11         a[i]=a[len-i-1];
12         a[len-i-1]=t;
13     }
14 }
15 
16 int main()
17 {
18     int alen, blen, s, len, i, j;
19     char a[260], b[260], r[1000], t;
20     while(scanf("%s%s%*c", a, b)==2)
21     {
22         memset(r, 0, sizeof(r));
23         alen=strlen(a);
24         blen=strlen(b);
25         func(a);
26         func(b);
27         for (i=0; i<alen; i++)
28         {
29             s=0;
30             for (j=0; j<blen; j++)
31             {
32                 t=r[i+j]+(a[i]-'0')*(b[j]-'0')+s;
33                 s=t/10;
34                 r[i+j]=t%10;
35             }
36             if (s > 0)
37                 r[i+j]+=s;
38         }
39         if (s > 0)
40             len=alen+blen;
41         else
42             len=alen+blen-1;
43         if (strcmp(a, "0") == 0 || strcmp(b, "0") == 0)
44             len = 1;
45         for (i=len-1; i>=0; i--)
46             printf("%c", r[i]+'0');
47         printf("\n");
48     }
49     return 0;
50 }
posted on 2012-06-16 00:29  zrq495  阅读(253)  评论(0编辑  收藏  举报