因为1000的阶乘太大了,int放不下,为了方便,你可以输入阶乘值在int范围内的任何数,一下是代码和运行结果
package com.java.xxx;
import java.util.Scanner;
/*
* 求1000!的结果中包含多少个0?注:1000! = 1×2×3×4×5×...×999×1000
*/
public class Test {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个数");
int inputNumber = sc.nextInt();
int count = 0;
int num1 = 1;
for(int i = 1; i <= inputNumber; i++) {
num1 *= i;
}
System.out.println(inputNumber + "的阶乘结果为:" + num1);
while(num1 > 0) {
int num = num1 % 10;
num1 /= 10;
if(num == 0) {
count++;
}
}
System.out.println(inputNumber + "的阶乘结果中0的个数为:" + count + "个");
}
} |