172. 阶乘后的零(172. Factorial zero)

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

提示 n! = n * (n – 1) * (n – 2) * … * 3 * 2 * 1

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/factorial-trailing-zeroes
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

import java.util.Scanner;

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

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNext()) {
            System.out.println(new Solution().trailingZeroes(in.nextInt()));
        }
    }
}
————————

Given an integer n, return n! The number of trailing zeros in the result.

Prompt n= n * (n – 1) * (n – 2) * … * 3 * 2 * 1

Source: leetcode
Link: https://leetcode-cn.com/problems/factorial-trailing-zeroes
The copyright belongs to Lingkou network. For commercial reprint, please contact the official authorization, and for non-commercial reprint, please indicate the source.

import java.util.Scanner;

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

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNext()) {
            System.out.println(new Solution().trailingZeroes(in.nextInt()));
        }
    }
}