LintCode 2. 尾部的零
- LintCode 2. 尾部的零
- 设计一个算法,计算出 n 阶乘中尾部零的个数。
样例
11! = 39916800
,因此应该返回 2。
Java 代码
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 sum = 0;
while (n > 0) {
n = n / 5;
sum = sum + n;
}
return sum;
}
}
参考资料