给定一个数n,求其阶乘的末尾有多少个0。
末尾为0的产生原因是阶乘中出现了含有因子为2、5的数。含有因子为2的数,即偶数,远远多于含有因子为5的数。因此求出有多少个5的倍数就可以了,n/5。
这只考虑了两个数相乘末尾为一个0的情况,如2*5,但是还有两个数相乘末尾为两个0、三个0;
末尾有两个零是因为有因子4和25,末尾有三个0是因为有因子8、125。。。
这样可以写出公式
程序实现也很简单,对输入的n,求其与5、25、125...的商,然后加起来就是了
int main()
{
int i;
while( 1 == scanf("%d",&i) )
{
int count = 0;
while (i!=0)
{
count += i/5;
i=i/5;
}
printf("%d\n",count);
}
return 0;
{
int i;
while( 1 == scanf("%d",&i) )
{
int count = 0;
while (i!=0)
{
count += i/5;
i=i/5;
}
printf("%d\n",count);
}
return 0;
}