问题描述:
设计一个算法,计算出n阶乘中尾部零的个数
您在真实的面试中是否遇到过这个题? Yes
样例
11! = 39916800,因此应该返回 2
code:(自己的,不喜勿喷)
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.
long long zeroSumCount = 0; // 尾部零的总数量
long long quotients = 1;
for(long long i=5; quotients>0; i=5)
{
quotients = n/i;
zeroSumCount += quotients;
}
return zeroSumCount;
}
};