Trailing Zeros 算法
Write an algorithm which computes the number of trailing zeros in n factorial.
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 result = trailingZeros(n-1)*n ;
long i = 0 ;
while(n != 0){
i += n/5 ;
n /= 5 ;
}
return i ;
}
}
本文介绍了一种计算阶乘中尾部零的数量的算法,通过迭代计算n除以5的商来确定零的数量,适用于编程挑战和理解数字特性。重点在于避免直接使用算术运算符实现,提升代码效率。
264

被折叠的 条评论
为什么被折叠?



