您的位置:首页 > 大数据 > 人工智能

[leetcode][math] Factorial Trailing Zeroes

2015-06-02 12:06 441 查看
题目;

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

Note: Your solution should be in logarithmic time complexity.
class Solution {
public:
/*n!=1*2*3*4*5*6*7*8*9*10*11...*(n-1)*n,*/
/*分析:只有2*5能使得阶乘的末尾产生0,而2的个数一定比5多,所以问题转化成了在这个阶乘额计算式中有多少个5,我们发现,每5个数之中就多一个5(5, 10, 15,20,25...),而且每25(5*5)个数之中又多一个5(之前算25的时候只算了其中一个5), 每125(5*5*5)个数之中又多一个5...*/

int trailingZeroes(int n) {
int res(0);
while(n){
res += n/5;
n /= 5;
}
return res;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: