LeetCode 172 阶乘后的零

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/jsszwc/article/details/88074755

题目:

https://leetcode-cn.com/problems/factorial-trailing-zeroes/

题意:

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

思路:

只有数字 2 5 2*5 才能使积的末尾有零, 10 20 10、20 类似数据均可以抽离出 2 5 2*5 ,所以只需求 n ! n! 中,可以抽离出多少对 2 5 2*5 即可,又因为肯定 5 5 的数量少,所以求 n ! n! 中因子 5 5 的个数

代码:

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

猜你喜欢

转载自blog.csdn.net/jsszwc/article/details/88074755