[] Leetcode zero after 172 factorial (to find the law)

Topic links: https://leetcode-cn.com/problems/factorial-trailing-zeroes/

Title Description

Given an integer n, returns the n! Number of zeros in the result mantissa.

Example 1:

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

Example 2:

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

Description : The time complexity of your algorithm should be O (log n).

Thinking

First topic means there are several end 0, of which only the end of only 2 * 5 0, so we can see blows other specialized data 25 and multiples thereof; a pair 5, and a 2, a 0 is generated, since the affirmative 2 than more than 5, so I just what you need to count because multiplication in the number of type 5

Code

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

Here Insert Picture Description

Guess you like

Origin blog.csdn.net/zjwreal/article/details/94621598