设计一个算法,计算出n阶乘中尾部零的个数
样例
11! = 39916800,因此应该返回 2
python:
class Solution:
"""
@param: n: An integer
@return: An integer, denote the number of trailing zeros in n!
"""
def trailingZeros(self, n):
# write your code here, try to do it without arithmetic operators.
if n == 0:
return 1;
x = 0
while n > 5:
x += n // 5
n = n // 5
return x
C++:
class Solution {
public:
/*
* @param n: A long integer
* @return: An integer, denote the number of trailing zeros in n!
*/
long long trailingZeros(long long n) {
// write your code here, try to do it without arithmetic operators.
if (n == 0)
{
return 1;
}
if(n > 0 && n < 5)
{
return 0;
}else{
return (n/5 + trailingZeros(n/5));
}
}
};