HDU 1042

来源:互联网 发布:数据库添加 insert 编辑:程序博客网 时间:2024/05/18 03:33
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
123
 

Sample Output
126


题目意思很简单,求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();       }    }}


0 0
原创粉丝点击