LeetCode. 阶乘后的零

题目要求:

给定一个整数 n,返回 n! 结果尾数中零的数量。

示例:

输入: 3
输出: 0
解释: 3! = 6, 尾数中没有零。

解法:

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

猜你喜欢

转载自www.cnblogs.com/leyang2019/p/11696107.html
今日推荐