牛客题霸 [有关阶乘的两个问题1] C++题解/答案

牛客题霸 [有关阶乘的两个问题1] C++题解/答案

题目描述

给定一个非负整数N,返回N!结果的末尾为0的数量

题解:

这个题有技巧
10=2*5,也就是说有一对2和5就会贡献一个0,但是2的数量远远大于5,所以只用统计五即可

代码:

class Solution {
public:
    /**
     * the number of 0
     * @param n long长整型 the number
     * @return long长整型
     */
    long long thenumberof0(long long n) {
        // write code here
        long long sum=0;
        while(n)
        {
            sum+=n/5;
            n/=5;
        }
        return sum;
    }
};
posted @ 2020-11-19 22:23  回归梦想  阅读(128)  评论(0编辑  收藏  举报