hdu 1066 Last nonzero Digit in N!【阶乘】

比赛时一直错贴的叶找的代码:

#include <string.h>
#include<stdio.h>
#include<algorithm>
using namespace std;
#define MAXN 10000
const int mod[20]={1,1,2,6,4,2,2,4,2,8,4,4,8,4,6,8,8,6,8,2};//列出钱20个的阶层最后一位
int ld(char* buf)
{
    int len = strlen(buf), a[MAXN], i, c, ret = 1;
    if(len==1)
        return mod[buf[0]-'0'];//如果是个位数直接输出
    for(i=0;i<len;i++)
        a[i]=buf[len-1-i]-'0';//将从首位开始把其化为整数
    for(;len;len-=!a[len-1])//
    {
        ret=ret*mod[a[1]%2*10+a[0]]%5;
        for(c=0,i=len-1;i>=0;i--)//从个位开始
            c=c*10+a[i],a[i]=c/5,c%=5;//第一个表示还原成整数,每个位上的数都除以5,C变为5的余数
    }
    return ret+ret%2*5;//返回的是最终的尾数不为零的那个数

}
int main()
{
    char a[1000];
    while(scanf("%s",&a)!=EOF)
    {
        printf("%d\n",ld(a));
    }
    return 0;
}



原文地址:https://www.cnblogs.com/freezhan/p/3219069.html