HDU 1042

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Masqueradey/article/details/52173867
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();
	       }
    }
}


猜你喜欢

转载自blog.csdn.net/Masqueradey/article/details/52173867