Factorial Trailing Zeroes (LeetCode)

Question:

https://oj.leetcode.com/problems/factorial-trailing-zeroes/

 

解答:

这题其实是求小于N的数中是5的倍数的个数。还要注意有些数的因子可能是多个5的情况,比如25,50,所以需要计算25的倍数的个数,同样需要计算125的倍数的个数,所有这些个数的总和就是我们要的结果。

class Solution {
public:
    int trailingZeroes(int n) {
        int count = 0;
        
        long long base = 5;
        while (1)
        {
            if (n < base)
                break;
            
            count += (n/base);
            
            base *= 5;
        }
        
        return count;
    }
};

 

posted @ 2015-01-11 15:34  smileheart  阅读(122)  评论(0)    收藏  举报