[leetcode]阶乘后的零(Factorial Trailing Zeroes)

阶乘后的零(Factorial Trailing Zeroes)

给定一个整数 n,返回 n! 结果尾数中零的数量。

示例 1:

输入: 3
输出: 0
解释: 3! = 6, 尾数中没有零。

示例 2:

输入: 5
输出: 1
解释: 5! = 120, 尾数中有 1 个零.

说明: 你算法的时间复杂度应为 O(log n) 

原题链接:https://leetcode-cn.com/problems/factorial-trailing-zeroes/

class Solution {
public:
    int trailingZeroes(int n) {
        int sum=0;
        while(n)
        {
            n=n/5;
            sum=sum+n;
        }
        return sum;
    }
};

此题有坑,所谓的0是结果最后面的0,出现在中间的不算(105这个0就不算)既然是尾数,那只有每5个才会出现一次0,所以就统计能出多少个5就好了

猜你喜欢

转载自blog.csdn.net/gcn_Raymond/article/details/84778855
今日推荐