每出现一个2和5,就会在末尾有一个0,所以只要看,从1
到1000中总共有多少个2和5就可以了,又因为5总比2少,所以,只要看1000的阶乘中有多少个约数5就可以了。同样,只有末尾是0或者5的数才会有5,所以总共只有200个数其中包含5,但是,其中有1000/25=40个数包含2个5,1000/125=8个数包含三个5,1000/625=1个数包含4个5,所以总共有200+40+8+1=249个5,所以结果里总共有249个0。
首先确定一项,能让末尾产生0的只有5的倍数和偶数相乘;
其次,偶数项个数一定要不小于是5的倍数的个数。
然后就是分层了。
%1000==0 count+3
%500==0 count+3
%100==0 count+2
%50==0 count+2
%10==0 count+1
%5==0 count+1
/*********************************************/
#include "stdafx.h"
int main ()
{
int i=1,count=0;
for (; i<1001; i++)
{
if (i % 1000 == 0)
count += 3;
else if (i % 500 == 0)
count += 3;
else if (i % 100 == 0)
count += 2;
else if (i % 50 == 0)
count += 2;
else if (i % 10 == 0)
count += 1;
else if (i % 5 == 0)
count += 1;
}
printf("%d",count);
getchar();
return 0;
}
#include
int main()
{
int d = 1000;
int i,j,m;
m = 1;
j = 0;
for(i=1; i<=d; i++)
{
m *= i;
while( m%10 == 0 )
{
j++;
m /= 10;
}
m = m % 1000;
}
printf("%d\n", j);
// printf("%d\n", m);
return 0;
}
这个算法有问题,我想的太简单了,不好意思