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.
Analysis:
参考:http://bookshadow.com/weblog/2014/12/30/leetcode-factorial-trailing-zeroes/ 和 http://www.cnblogs.com/ganganloveu/p/4193373.html
两个其实说的是一个意思,一种方法。
Source Code(C++):
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class Solution {
public:
int trailingZeroes(int n) {
int five_counts=0;
while(n>0) {
five_counts += n/5;
n /= 5;
}
return five_counts;
}
};
int main() {
Solution sol;
cout << sol.trailingZeroes(50);
return 0;
}