172. Zero after factorial

Title description

Given an integer n, return n! The number of zeros in the mantissa of the result.

Example

示例 1:

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

示例 2:

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

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

Problem solving ideas

This question examines the factorial result with several zeros at the end, such as 103030 is 1 and 100 is 2; analyze the factorial factor: only the multiples of X5*2 can form the tail 0, it is easy to understand that the multiples of 2 are more than the factors of type X5 , So count the number of 5^i. For
example, the code: count+=(int)(n/pow(5,i));

Code

int trailingZeroes(int n){
    
    
    int count=0;

    for(int i=1;pow(5,i)<=n;i++){
    
    
        count+=(int)(n/pow(5,i));
    }return count;

}

link

Guess you like

Origin blog.csdn.net/qq_44722674/article/details/112134885