思路
能产生0的质数组合只有2*5,然后问题就转变成了对N!进行质数分解后,一共有几个5,因为2的个数显然多于5。比如计算25!的末尾0的个数,包含5的数有5,10,15,20,25,其中25中包含两个5,所以一共包含6个5,25!的末尾有6个0.
Java代码
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int count = 0, j;
for (int i = 1; i <= n; i++) {
j = i;
while (j % 5 == 0) {
count++;
j = j / 5;
}
}
System.out.println(count);
}
}