【每天一道编程系列-2018.3.14】—— Trailing Zeros

2019-03-14 12:50:26 浏览数 (3)

【题目描述】

 O(log N) time

Write an algorithm which computes the number of trailing zeros in n factorial.

Have you met this question in a real interview?

Yes

Example

11! = 39916800, so the out should be 2

【题目大意】

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

【本题答案】

代码语言:javascript复制
package blog;

/**
 * @author yesr
 * @create 2018-03-14 下午10:44
 * @desc
 **/
public class Test0314 {
    /*
     * param n: As desciption return: An integer, denote the number of trailing
     * zeros in n!
     */
    public long trailingZeros(long n) {
        // write your code here
        long count = 0;
        long temp=n/5;
        while (temp!=0) {
            count =temp;
            temp/=5;
        }
        return count;
    }
}

0 人点赞