[CareerCup] 17.3 Factorial Trailing Zeros 求阶乘末尾零的个数
LeetCode上的原题,讲解请参见我之前的博客Factorial Trailing Zeroes。
解法一:
int trailing_zeros(int n) { int res = 0; while (n) { res += n / 5; n /= 5; } return res; }
解法二:
int trailing_zeros(int n) { return n == 0 ? 0 : n / 5 + trailing_zeros(n / 5); }