leetcode 172

172. Factorial Trailing Zeroes

Given an integer n, return the number of trailing zeroes in n!.

Note: Your solution should be in logarithmic time complexity.

计算出n!中尾部0的个数。1*2*3*......*n中0的个数由2和5的对数决定,由于因子5的个数小于因子2的个数,所以只需求出因子5 的个数即为尾部0的个数。

n/5可以求得能被5整除数的个数;

但25中存在两个因子,125中存在三个因子,依次类推。

所以在求因子5的时候应该考虑到。

代码如下:

 1 class Solution {
 2 public:
 3     int trailingZeroes(int n) {
 4         int count = 0;
 5         while(n)
 6         {
 7             count += n/5;
 8             n /= 5;
 9         }
10         return count;
11     }
12 };
原文地址:https://www.cnblogs.com/shellfishsplace/p/6043585.html