尾部的零
设计一个算法,计算出n阶乘中尾部零的个数
样例
样例 1:
输入: 11
输出: 2
样例解释:
11! = 39916800, 结尾的0有2个。
样例 2:
输入: 5
输出: 1
样例解释:
5! = 120, 结尾的0有1个。
// An highlighted block
public class Solution {
/*
* @param n: An integer
* @return: An integer, denote the number of trailing zeros in n!
*/
public long trailingZeros(long n) {
// write your code here, try to do it without arithmetic operators.
long count = 0;
while (n >= 5) {
count += n / 5;
n /= 5;
}
return count;
}
public static void main(String[] args) {
Solution solution = new Solution();
long zeros = solution.trailingZeros(1001171717);
System.out.println(zeros);
}
}