思路:5*2=10,即每个5与2都能产生一个0,而在阶乘中,偶数很多,即2比5多
1-N中有N/5个数,每个数能贡献一个5,1-N有N/5^2个数,每个数又能贡献一个5等等
import java.util.Scanner;
public class 阶乘中0的个数 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scanner = new Scanner(System.in);
int N = scanner.nextInt();
System.out.println(getZeroCounts(N));
}
/**
* 思路是5*2=10,即每个5与2都能产生一个0,而在阶乘中,偶数很多,即2比5多
* 1-N中有N/5个数,每个数能贡献一个5,1-N有N/5^2个数,每个数又能贡献一个5等等
* 即count = N/5+N/5^2+...+N/5^i
* @param n
* @return
*/
private static int getZeroCounts(int N) {
// TODO Auto-generated method stub
int i = 1;
int count = 0;
while(N/(Math.pow(5, i))>0){
count+=N/(Math.pow(5, i));
i++;
}
return count;
}
}