码迷,mamicode.com
首页 > 其他好文 > 详细

阶乘中末尾0的个数

时间:2020-12-03 12:24:54      阅读:14      评论:0      收藏:0      [点我收藏+]

标签:code   while   整数   div   rgs   有关   str   long   返回   

public class jiecheng {
    /**
     * 给定一个非负整数N,返回N!结果的末尾为0的数量
     * @param args
     */
    public static void main(String[] args) {
        thenumberof0(5);
    }
    /**
     * the number of 0
     * @param n long长整型 the number
     * @return long长整型
     */
    //要相乘产生0,那肯定是与5相乘的结果。对n!如果分解质因数的话,结果为0的个数只与2与5的个数有关,每一次2*5就能产生一个0。因为2的个数肯定要大于5的个数,所以只要关注5的个数就可以了.
    public static long thenumberof0 (long n) {
        // write code here
        long ret=0;
        long i=5;
        while(i<=n){
            ret=ret+n/i;
            i=i*5;
        }
        return ret;
    }
    
}

  

阶乘中末尾0的个数

标签:code   while   整数   div   rgs   有关   str   long   返回   

原文地址:https://www.cnblogs.com/jieyi/p/14056955.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!