阶乘末尾的零个数
[编程题] 末尾0的个数
输入一个正整数n,求n!(即阶乘)末尾有多少个0? 比如: n = 10; n! = 3628800,所以答案为2
输入描述:
输入为一行,n(1 ≤ n ≤ 1000)
输出描述:
输出一个整数,即题目所求
输入例子:
10
输出例子:
2
方法:Z = N/5 + N /(5*5) + N/(5*5*5).....知道N/(5的K次方)
1 import java.util.Scanner; 2 3 public class Test60 { 4 5 public static void main(String[] args) { 6 7 Scanner in = new Scanner(System.in); 8 int n = in.nextInt(); 9 int count = 0; 10 while(n>0){ 11 count += n/5; 12 n = n/5; 13 } 14 System.out.println(count); 15 in.close(); 16 } 17 18 }
Jumping from failure to failure with undiminished enthusiasm is the big secret to success.