题解 | 阶乘末尾0的数量

阶乘末尾0的数量

https://www.nowcoder.com/practice/aa03dff18376454c9d2e359163bf44b8

import java.util.*;

/**
    关键:n!末尾出现零只有中途乘了10才有可能,而乘了几个10,就会有几个0。
    而10 = 2 * 5,也就是现在要从阶乘的乘数因子中找到成对的2和5。
    而乘中的因子2的个数比5的个数多很多,所以2不需要找了,只要找5的个数。
    即5的个数就是10的个数也就是末尾0的个数阶。
 */

public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * the number of 0
     * @param n long长整型 the number
     * @return long长整型
     */
    public long thenumberof0 (long n) {
        // write code here\
        long count = 0;
        while (n / 5 != 0) {
            count = count + n / 5;
            n /= 5;
        }
        return count;
    }
}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务