题解 | #阶乘末尾0的数量#

阶乘末尾0的数量

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

import java.util.*;

//数学性质:只有5的倍数与2的倍数相乘才能得出10的倍数,经观察含2的倍数比含5的倍数要多;
//即求出n有多少个含5的倍数就是题目所求:阶乘末尾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;
        long m = 5;
        while(n>=m){
            count += n/m;
            m = m*5;
        }
        
        return count;
    }
}
全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

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