首页 > 其他 > 详细

阶乘尾零

时间:2017-04-22 09:40:54      阅读:216      评论:0      收藏:0      [点我收藏+]

题目描述

请设计一个算法,计算n的阶乘有多少个尾随零。

给定一个int n,请返回n的阶乘的尾零个数。保证n为正整数。

测试样例:
5
返回:1
class Factor {
public:
    int getFactorSuffixZero(int n) {
        // write code here
        int tmp = n;
        int count2 = 0;
        int count5 = 0;
        
        while(tmp){
            count2 += tmp/2;
            tmp /= 2;
        }
        
        tmp = n;
        while(tmp){
            count5 += tmp/5;
            tmp /= 5;
        }
        
        return count2>count5?count5:count2;
    }
};

 

阶乘尾零

原文:http://www.cnblogs.com/xiuxiu55/p/6746726.html

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