lintcode-2-尾部的零

尾部的零

设计一个算法,计算出n阶乘中尾部零的个数

样例

11! = 39916800,因此应该返回 2

挑战

O(logN)的时间复杂度

标签

数学

思路

参考文章:http://m.blog.csdn.net/article/details?id=51168272

code

class Solution {
public:
    /**
     * @param n : description of n 
     * @return: description of return
     * @cankao: http://m.blog.csdn.net/article/details?id=51168272
     */
    long long trailingZeros(long long n) {
        
        long long count = 0, temp = n/5;

        while(temp != 0 ) {
            count += temp;
            temp /= 5;
        }
        return count;
    }
};
原文地址:https://www.cnblogs.com/libaoquan/p/6979715.html