码迷,mamicode.com
首页 > 其他好文 > 详细

Factorial Trailing Zeroes

时间:2015-03-17 19:47:19      阅读:136      评论:0      收藏:0      [点我收藏+]

标签:

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

Note: Your solution should be in logarithmic time complexity.

解析: 只有2和5相乘才会出现0,其中整十也可以看做是2和5相乘的结果,所以,可以在n之前看看有多少个2以及多少个5就行了,又发现2的数量一定多于 5的个数,于是我们只看n前面有多少个5就行了,于是n/5就得到了5的个数,还有一点要注意的就是25这种,5和5相乘的结果,所以,还要看n/5里面 有多少个5,也就相当于看n里面有多少个25,还有125,625.。

 

int trailingZeroes(int n) {
    int count = 0;
    while(n){
        count = count+n/5;
        n /=5;
    }
    return count;
}

 

 

 

Factorial Trailing Zeroes

标签:

原文地址:http://www.cnblogs.com/zhhc/p/4345135.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!