N! (n的階乘)(大數階乘)
阿新 • • 發佈:2018-11-29
Description
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.BigInteger; import java.util.Scanner; public class Main { public static void main(String[] args) { // TODO Auto-generated method stub Scanner in = new Scanner(System.in); while(in.hasNext()) //多組輸入輸出 { BigInteger sum = new BigInteger("1");//對sum賦初始值 int n = in.nextInt(); //當n超過30時,就需要用大數類 int i; for(i = 1; i <= n; i++) { BigInteger num = new BigInteger(String.valueOf(i));//將i變成大數形式 sum = sum.multiply(num); } System.out.println(sum); } } }