動態:#yyds干貨盤點# 名企真題專題:末尾0的個數

2022-12-28 16:19:20 來源:51CTO博客


(相關資料圖)

1.簡述:

描述

輸入一個正整數n,求n!(即階乘)末尾有多少個0? 比如: n = 10; n! = 3628800,所以答案為2

輸入描述:

輸入為一行,n(1 ≤ n ≤ 1000)

輸出描述:

輸出一個整數,即題目所求

示例1

輸入:

10

輸出:

2

2.代碼實現:

public class Main{    public static int test(int n){        if(n < 0){            return  0;        }        int res = 0;        while (n !=0){            res += n /5;            n = n/5;        }        return res;    }       public static void main(String[] args) {        Scanner in = new Scanner(System.in);        while (in.hasNextInt()) {            System.out.println( test(in.nextInt()));        }    }}

標簽:

上一篇:微頭條丨#yyds干貨盤點# 名企真題專題:直方圖內最大矩形
下一篇:世界訊息:隱私計算之多方安全計算(MPC,Secure Multi-Party Computation)