题目描述
设计一个算法,计算出n阶乘中尾部零的个数
分析
这其实是一个数学题,只有2和5这两个因子相乘才会在尾部产生0,2的数量比5的数量多得多,因此只需统计所有数中因子5有多少个就行了。
代码
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 res = 0;
while(n>0){
res+=n/5;
n=n/5;
}
return res;
}
}