Zero after factorial

Title: https://leetcode-cn.com/problems/factorial-trailing-zeroes/ The
essence is to find the number of 5 in n!
The way of thinking: first find the number with 1 and 5 n / 5 n/5 , and then find the number with 2 5s n / 5 2 n/5^2

class Solution {
public:
    int trailingZeroes(int n) {
        int res = 0;
        while(n) {
            res += n/5;
            n /= 5;
        }
        return res;
    }
};
Published 152 original articles · praised 2 · visits 6450

Guess you like

Origin blog.csdn.net/weixin_43918473/article/details/104693486