阶乘最后非零位

 1 #include<string.h>
 2 #define MAXN  10000
 3 int lastdigit(char*buf)
 4 {
 5 const int mod[20] = {1,1,2,6,4,2,2,4,2,8,4,4,8,4,6,8,8,6,8,2};
 6 int len = strlen(buf),a[MAXN],i,c,ret = 1;
 7 if(len == 1)
 8 return mod[buf[0]-'0'];
 9 for(i = 0;i<len;i++)
10 a[i] = buf[len-1-i]-'0';
11 for(;len;len-=!a[len-1])
12 {
13 ret = ret*mod[a[1]%2*10+a[0]]%5;
14 for(c = 0,i = len-1;i>=0;i--){
15 c = c*10+a[i];
16 a[i] = c/5;
17 c%=5;
18 }
19 }
20 return ret+ret%2*5;
21 }

posted on 2012-07-08 10:53  dinghing  阅读(330)  评论(0编辑  收藏  举报

导航