开源地址: 点击该链接 https://github.com/JiauZhang/algorithms
题目描述
https://leetcode-cn.com/problems/factorial-trailing-zeroes
给定一个整数 n, 返回 n! 结果尾数中零的数量.
示例 1:
输入: 3
输出: 0
解释: 3! = 6, 尾数中没有零.
示例 2:
输入: 5
输出: 1
解释: 5! = 120, 尾数中有 1 个零.
说明:
你算法的时间复杂度应为 O(logn).
示例代码
- class Solution {
- public:
- int trailingZeroes(int n) {
- int max_exp = log(n) / log(5);
- int res = 0;
- for (int i=0; i<max_exp; i++) {
- int temp = n / 5;
- res += temp;
- n = temp;
- }
- return res;
- }
- };
来源: http://www.bubuko.com/infodetail-3257288.html