方式一:
import java.math.BigDecimal;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (scanner.hasNext()) {
int n = scanner.nextInt();
BigDecimal num = BigDecimal.ONE;
for (int i = 2; i <= n; i++) {
num = num.multiply(BigDecimal.valueOf(i));
}
System.out.println(num);
}
}
}
方式二:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
if (n > 1000)
return;
// int sum = 0;
int sum[] = new int[10000];
for (int i = 0; i < sum.length; i++) {
sum[i] = 0;
}
sum[0] = 1;
// System.out.println(sum.length);
for (int i = 1; i <= n; i++) {
for (int j = 0; j < sum.length; j++) {
sum[j] = sum[j] * i;
}
for (int k = 0; k < sum.length; k++) {
if (sum[k] >= 10) {
int kinwei = sum[k] / 10;
sum[k + 1] += kinwei;
sum[k] = sum[k] % 10;
}
}
}
// System.out.println(sum);
// for (int i = 0; i < 10; i++) {
// System.out.print(sum[i]);
// }
int flag = 1;
for (int i = sum.length - 1; i >= 0; i--) {
if (flag == 1) {
if (sum[i] != 0)
flag = 0;
}
if (flag == 0) {
System.out.print(sum[i]);
}
}
}
}