这几天一直在做高精度的题目  乘法我也是直接模拟,总觉的我的程序有些地方不必要,今天看到一个好的代码 ,贴上来

我的方法

View Code
 1 void mu(int c[],int d[], int e[])
 2 { 
 3     void print();
 4     int i ,j, k ,t ; 
 5     for (i =0 ;i < strle(c) ;i ++)
 6     {
 7         int s = 0 ;
 8         for (j = 0 ; j < strle(d) ;j++)
 9         {
10             t = c[i]*d[j] +s +e[i+j] ;
11             s = t / 10 ;
12             e[i+j] = t % 10 ;
13         }
14         for (j = i+j  ; j< 550 ; j++)
15         {
16             t = e[j] + s;
17             s = t /10 ;
18             e[j] = t %10 ;
19         }
20 
21     }

她的做法:http://sumile.blog.hexun.com/62509182_d.html

View Code
 1 void multiply(char* a,char* b,char* c)
 2 {
 3     int i,j,ca,cb,*s;
 4     ca=strlen(a);
 5     cb=strlen(b);
 6     s=(int*)malloc(sizeof(int)*(ca+cb));
 7     for (i=0;i<ca+cb;i++)
 8         s[i]=0;
 9     for (i=0;i<ca;i++)
10         for (j=0;j<cb;j++)
11             s[i+j+1]+=(a[i]-'0')*(b[j]-'0');
12     for (i=ca+cb-1;i>=0;i--)
13         if (s[i]>=10)
14         {
15             s[i-1]+=s[i]/10;
16             s[i]%=10;
17         }
18     i=0;
19     while (s[i]==0)
20         i++;
21        for (j=0;i<ca+cb;i++,j++)
22            c[j]=s[i]+'0';
23     c[j]='\0';
24     free(s);
25 }

 

posted on 2013-01-27 17:12  dark_dream  阅读(208)  评论(0编辑  收藏  举报