当前位置:网站首页>172. Zero after factorial

172. Zero after factorial

2022-07-05 01:42:00 The_ Dan

Think first 0 How it came about : factor 2 x factor 5 Will appear at the end 0
So do we calculate the factor in the process 2 And factor 5 The number of is enough ?
But in fact, calculation 5 How many times can it appear , Because in the process 2 The number of must be higher than 5 A lot more , It's easy to understand , There will be at least one two in even numbers .

class Solution {
    
public:
    int trailingZeroes(int n) {
    
        int ans;
        int five = 0;
        while(n){
    
            int temp = n;
            while(temp % 5 == 0){
    
                five++;
                temp /= 5;
            }
            n--;
        }
        return five;
    }
};

Accepted
500/500 cases passed (0 ms)
Your runtime beats 100 % of cpp submissions
Your memory usage beats 66.85 % of cpp submissions (5.8 MB)

原网站

版权声明
本文为[The_ Dan]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202141019325025.html