首页 > 其他 > 详细

Factorial Trailing Zeroes

时间:2014-12-31 08:40:08      阅读:197      评论:0      收藏:0      [点我收藏+]

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;
}

 

Factorial Trailing Zeroes

原文:http://www.cnblogs.com/wuchanming/p/4194960.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!