n的階乘後面有多少個0?java
6的階乘 = 1*2*3*4*5*6 = 720,720後面有1個0。spa
Input一個數N(1 <= N <= 10^9)Output輸出0的數量Sample Inputcode
5
Sample Outputblog
1
思路:有5做爲乘數才能產生末尾0,求末尾有多少個0至關於求因子5的個數
n不斷除以5
第一次除以5獲得是1~n中因子含5的個數
第二次除以5獲得的是1~n中因子含25的個數
...
第n次除以5獲得的是1~n中因子含5^n的個數
代碼:
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scan=new Scanner(System.in); int n=scan.nextInt(); int count=0; while(n>0){ count+=(n=n/5); } System.out.println(count); } }