1. 程式人生 > >HDU 1042

HDU 1042

Problem 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
   
  
 


題目意思很簡單,求N的階乘,因為到10000顯然要用大數做,

個人感覺大數的題用JAVA做比較好,畢竟寫起來比較省時間。

程式碼:

import java.util.Scanner;
import java.math.*;
public class Main {
	public static void main(String[] args) {
		Scanner cin = new Scanner(System.in);
		while(cin.hasNext()){
                      int  a=cin.nextInt();
                      BigInteger b=new BigInteger("1");
                      for(int i=1;i<=a;i++)
                      {
                        BigInteger num = new BigInteger(i+"");
                        //把整數變成大數的一個比較巧妙的方法
                        b=b.multiply(num);
		      }
                   System.out.println(b.toString());
                   System.gc();
	       }
    }
}