首页 > 代码库 > Factorial Trailing Zeroes

Factorial Trailing Zeroes

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

Note: Your solution should be in logarithmic time complexity.

类似于cc:http://www.cnblogs.com/wuchanming/p/4158567.html

5!, 包含1*5, 1个510!, 包含1*5,2*5, 2个515!, 包含1*5,2*5,3*5, 3个520!, 包含1*5,2*5,3*5,4*5, 4个525!, 包含1*5,2*5,3*5,4*5,5*5, 6个5

 

C++实现代码:

#include<iostream>using namespace std;class Solution {public:    int trailingZeroes(int n) {        if(n==0)            return 0;        int count=0;        while((n=n/5)>0)        {            count+=n;        }        return count;    }};int main(){    Solution s;    cout<<s.trailingZeroes(25)<<endl;}

 

Factorial Trailing Zeroes