1. 程式人生 > >Leetcode 172.階乘後的零

Leetcode 172.階乘後的零

階乘後的零

給定一個整數 n,返回 n! 結果尾數中零的數量。

示例 1:

輸入: 3

輸出: 0

解釋: 3! = 6, 尾數中沒有零。

示例 2:

輸入: 5

輸出: 1

解釋: 5! = 120, 尾數中有 1 個零.

 

 

 1 class Solution {
 2     public static int trailingZeroes(int n) {
 3         int sum=0;
 4         while(n>0){
 5             sum+=n/5;
6 n/=5; 7 } 8 return sum; 9 } 10 public static void main(String[] args){ 11 trailingZeroes(25); 12 } 13 }