尾部的零

算出n阶乘中尾部的零。

一开始看到这个题目都没仔细想就按照题目的意思大概写了出来,但是,虽然能运行,但是被写的复杂化了。。。。。

后来在网上看了下算法大神的,被打击的不要不要的。

/**
 * 尾部的零
 * ---2*5才会产生零,所以可以由5的个数决定零的个数
 * @author lzw
 */
public class trailingZeros_true {
    public static void main(String[] s) {
        System.out.println(trailingZeros(105));
    }
     public static long trailingZeros(long n) {
            long sum = 0;
            while (n != 0) {
                sum += n / 5;
                n /= 5;
            }
            return sum;
        }
}
原文地址:https://www.cnblogs.com/liamlee/p/9443967.html