有零说明可以%10==0 ,转化为10 = 2 * 5;所以有一对2*5就有一个0,
容易知道N!= N *(N-1)*(N-2)*(N-3)...*1;
并且因子中肯定是 2比5 多,所以只要计算5做为因子的数量,即可求出末尾0的数量。
即:假设所求总数为SUM 则有: N! = POW(5,SUM)*C 其中C是出了5之外的所有因子的乘积.
而零的数量 f(N) = n/5 + n/25 + n/125 + n/625…………;
public class yes {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
System.out.println(n+"的阶乘末尾有"+yes.fun(n)+"个0");
}
public static int fun(int n) {
int x = n;
int count=0;
while(x/5!=0) {
count += x/5;
x /=5;
}
return count;
}
}