/**
* 求1000!的结果中包含多少个0?注:1000! = 1×2×3×4×5×...×999×1000
* 由于1000太大了,超出int和long的长度,代码原理一样
*/
package com_01;
import java.util.Scanner;
public class JCDemo {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个数,求它的阶乘");
int jc = jc(sc.nextInt());
//打印12!的结果
System.out.println(jc);
String st = Integer.toString(jc);
char[] chs = st.toCharArray();
//记录0的个数
int count = 0;
for(char ch : chs){
if(ch == '0'){
count++;
}
}
//
System.out.println("count :"+count);
}
public static int jc(int num) {
if (num == 1) {
return 1;
} else {
return num * jc(num - 1);
}
}
}
|