Factorial Trailing Zeroes问题及解法

来源:互联网 发布:苹果手机铃声软件排行 编辑:程序博客网 时间:2024/06/16 22:21

问题描述:

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

问题分析:

n的阶乘后面有几个零,可根据n以内5的幂决定,举个例子,n=100时,5的倍数有20个,而这20个中5的倍数有4个,最后这4个中5的倍数有0个,所以n=100是,0的个数是24.

以此类推。


过程详见代码:

class Solution {public:    int trailingZeroes(int n) {        int res = 0;        while(n)        {        res += n / 5;        n = n / 5; }return res;    }};


0 0
原创粉丝点击