Given an integer n, return the number of trailing zeroes in n!.
Note: Your solution should be in logarithmic time complexity.
思路:大意是求n!的末尾有多少个0,可以想到要在末尾产生0,就必须有10才行,10=5*2,在阶乘中不缺2,那么这个问题就变成了在这个阶乘中有多少个5的问题
代码:
class Solution {
public:
int trailingZeroes(int n) {
int ans=0;
while(n){
ans+=(n/5);
n/=5;
}
return ans;
}
};