Given an integer N(0 ≤ N ≤ 10000), your task is to calculate N!
Input
One N in one line, process to the end of file.
Output
For each N, output N! in one line.
Sample Input
1
2
3
Sample Output
1
2
6
代码:
import java.math.BigDecimal;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
int n = sc.nextInt();
System.out.println(fun(n));
}
}
public static BigDecimal fun(int n) {
BigDecimal s = new BigDecimal(1);
for (int i = 1; i <= n; i++) {
BigDecimal a = new BigDecimal(i);
s = s.multiply(a);
}
return s;
}
}
本文介绍了一个使用Java实现的大数阶乘计算程序。该程序能够处理0到10000之间的整数N,并准确计算其阶乘。通过使用BigDecimal类,确保了计算的精度,适用于需要高精度数学运算的场景。
199

被折叠的 条评论
为什么被折叠?



