题目;
Given an integer n, return the number of trailing zeroes in n!.
Note: Your solution should be in logarithmic time complexity.
class Solution {
public:
/*n!=1*2*3*4*5*6*7*8*9*10*11...*(n-1)*n,*/
/*分析:只有2*5能使得阶乘的末尾产生0,而2的个数一定比5多,所以问题转化成了在这个阶乘额计算式中有多少个5,我们发现,每5个数之中就多一个5(5, 10, 15,20,25...),而且每25(5*5)个数之中又多一个5(之前算25的时候只算了其中一个5), 每125(5*5*5)个数之中又多一个5...*/
int trailingZeroes(int n) {
int res(0);
while(n){
res += n/5;
n /= 5;
}
return res;
}
};