题目:
给定一个以字符串方式输入的大数,求其阶乘最后非零位。
代码:
#include<cstdio>
#include<cstring>
#define maxn 10000
int Last_Digit(char *buf)
{
const int mod[20] = {1,1,2,6,4,2,2,4,2,8,4,4,8,4,6,8,8,6,8,2};
int len = strlen(buf),a[maxn],ret = 1;
if(len == 1)
{
return mod[buf[0]-'0'];
}
for(int i = 0;i < len;i++)
{
a[i] = buf[len-1-i] - '0';
}
for(;len != 0;len -= !a[len-1])
{
ret = ret*mod[a[1]%2*10 + a[0]]%5;
for(int c = 0,i = len-1;i >= 0;i--)
{
c = c*10 + a[i];
a[i] = c/5;
c %= 5;
}
}
return ret + ret %2*5;
}
int main()
{
char buf[maxn];
while(~scanf("%s",buf))
{
int ans = Last_Digit(buf);
printf("%d\n",ans);
}
}
(这个龙龙代码不太明白,望大牛指点。~(-_- )~)