2. Trailing Zeros

2. Trailing Zeros

Description

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

Example

11! = 39916800, so the out should be 2

Solution

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

}
阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/foradawn/article/details/79945733
文章标签: Mathematics
个人分类: LintCode
上一篇7. Serialize and Deserialize Binary Tree
下一篇8. Rotate String
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭