LintCode-简单 2.尾部的0

2. 尾部的零

设计一个算法,计算出n阶乘中尾部零的个数

样例

样例  1:
	输入: 11
	输出: 2
	
	样例解释: 
	11! = 39916800, 结尾的0有2个。

样例 2:
	输入:  5
	输出: 1
	
	样例解释: 
	5! = 120, 结尾的0有1个。

public class Solution {
    /*
     * @param n: An integer
     * @return: An integer, denote the number of trailing zeros in n!
     */
    public long trailingZeros(long n) {
        // write your code here, try to do it without arithmetic operators.
        long num = 0;
        while(n >= 5){
            num += n/5;
            n = n/5;
        }
        return num;
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_44977914/article/details/89971639