我想起来以前有一道题:n!最后一位是几?
1! = 1;
2! = 2;
3! = 6;
4! = 24;
5! = 120 = 1*2*3*4*5 = 1*3*4*2*5;
当n>5时,最后一位必为0。
所以这道题也是类似,只需要看有多少个2*5就好了。因为在阶乘中, 2比5多,所以只需要看有多少个5。
/**
* @param {number} n
* @return {number}
*/
var trailingZeroes = function(n) {
if (n < 5) return 0;
return Math.floor(n / 5 + trailingZeroes(Math.floor(n / 5)));
};