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个5

10!, 包含1*5,2*5, 2个5

15!, 包含1*5,2*5,3*5, 3个5

20!, 包含1*5,2*5,3*5,4*5, 4个5

25!, 包含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;

}

 

你可能感兴趣的:(zero)