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;
    }
}

猜你喜欢

转载自blog.csdn.net/king1994wzl/article/details/82977728
今日推荐