思路
能産生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);
}
}