2. Trailing Zeros

題目

https://www.lintcode.com/problem/trailing-zeros/description?_from=ladder&&fromId=2

實現

因爲 0 的都是由 2 * 5 得來的,而 5 出現的次數要大於 2 出現的次數,直接計算 5 因子的個數即可。

所以只要每次 sum up 可以被 5 整除的數就等於 5 這個素因子出現多少次

代碼

class Solution:
    """
    @param: n: An integer
    @return: An integer, denote the number of trailing zeros in n!
    """
    def trailingZeros(self, n):
        count = 0

        while n != 0:
            count += n / 5
            n /= 5

        return count
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章