原题如下:
2. Trailing Zeros
Write an algorithm which computes the number of trailing zeros in n factorial.
Example
11! = 39916800, so the out should be 2
Challenge
O(log N) time
这题挺有意思。因为阶乘里面的0都是由2*5得来的,所以看有多少个(2,5)构成的pair就可以了。而我们知道2的数目远远多于5的数目,所以只看5的数目就可以了。
比如说105的 阶乘,里面有多少个5呢? 首先5,10,15,20,…105这里面每个都至少有1个5,而且有些有2个,3个, 甚至更多。我们用个循环把它们都加起来就可以了。
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) {
long long count = 0; //count of 5
while (n) {
n /= 5;
count += n;
}
return count;
}
};
本文介绍了一种计算n!阶乘尾数中零的数量的高效算法。通过分析可知,阶乘尾数的零主要由2和5的配对产生,而5的数量远少于2,故只需关注5的个数。文章给出了一段C++代码实现,采用循环除以5的方式,统计n!中5的因子总数,从而得到尾数中零的数量。
3001

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



