JAVA求一个整数阶乘末尾0的个数

请听题

======================================================================================================

时间限制:1秒

空间限制:32768K


输入一个正整数n 求n!的结尾有多少个0?

比如n=10; n!=3628800,所以答案为2


输入描述:

输入n(1<=n<=1000),如:10

输出描述:

输出一个整数,及0的个数,如:2


======================================================================================================

在头条里看到说是滴滴的面试题,总共两个,第一个题目太长,懒得看,所以写了第二个,一群评论的二货写的答案是 n/5,可是验证过了,肯定不对。

可是我不知道这个答案用数学公式该怎么来写,直接上代码。


	
/**
 * 第二题的解法
 * 
 * @author LvGuoFei
 * @version 创建时间:2016-9-18 下午11:37:54
 * @version <h3>This class of code that there will be no mistake, amen</h3>
 */
public class Reply {
    /**
     * 统计阶乘数据中出现零的个数
     * 
     * @param i
     * @return
     * @author LvGuoFei
     */
    private static int zeroNum(int i) {
        int zn = i / 5;
        int aa = cs(zn);
        aaj = 0;
        zn = zn + aa;
        return zn;
    }

    private static int aaj = 0;

    private static int cs(int i) {
        int j = i / 5;
        if (j >= 5) {
            aaj += j;
            cs(j);
        } else {
            aaj += j;
        }
        return aaj;
    }

    public static void main(String[] args) {
        System.out.println(zeroNum(1259));
    }

}



还是没想起来怎么写公式,谁知道了求告知。

猜你喜欢

转载自blog.csdn.net/zhouzhupianbei/article/details/52579620
今日推荐