求N!末尾有多少个零

来源:百度知道 编辑:UC知道 时间:2024/05/23 02:21:54
#include<stdio.h>
main()
{
int a,count=0,i;
printf("input a:\n");
scanf("%d",&a);
for(i=5;i<=a;i+=5)
{
count++;
if(!(i%25))
count++;
if(!(i%125))
count++;
if(!(i%625))
count++;
}
printf("The number of 0 in the end of 100! is:%d.",count);
}
算法没有错误,我想降低它的时间复杂度,试了多种方法总是不行,请高手指点一下

正好看过这个的算法,2*5=10,在一个数N中,因子2出现的次数总比5出现的次数多,比如说10!
零的个数可以由这样算出
10/5=2
2/5=0
0的个数就是2+0=2,再来个例子,2008
2008/5=401
401/5=80
80/5=16
16/5=3
0的个数就是401+80+16+3=500个零