172. Factorial Trailing Zeroes
问题描述:
Given an integer n, return the number of trailing zeroes in n!.
Example 1:
Input: 3 Output: 0 Explanation: 3! = 6, no trailing zero.
Example 2:
Input: 5 Output: 1 Explanation: 5! = 120, one trailing zero.
Note: Your solution should be in logarithmic time complexity.
解题思路:
要求阶乘中后面0的个数。
2*5=10 每有一个2和5就会有一个0。
注意15, 25等5的倍数也可能构成0,
所以我们算n中有几个5.
代码:
class Solution { public: int trailingZeroes(int n) { int ret = 0; while(n){ ret += n/5; n /= 5; } return ret; } };