1. 程式人生 > >LeetCode_172. 階乘後的零

LeetCode_172. 階乘後的零

求末尾多少0可以表示為 n*10—>n*2*5。即求分解後有多少個5.
public class S_172 {
    public int trailingZeroes(int n) {
        // 提出一個特殊情況
        if (n <= 1) {
            return 0;
        }
        int result = 0;
        while (n != 0) {
            result += n / 5;
            // 注意:需要得到每個數分解質因子後5的個數
            n = n / 5;
        }
        return result;
    }
}