623 - 500!

UVa網站題目連結
My Solved Problems Performance

In these days you can more and more often happen to see programs which perform some useful calculations being executed rather then trivial screen savers. Some of them check the system message queue and in case of finding it empty (for examples somebody is editing a file and stays idle for some time) execute its own algorithm.

As an examples we can give programs which calculate primary numbers.

One can also imagine a program which calculates a factorial of given numbers. In this case it is the time complexity of order O(n) which makes troubles, but the memory requirements. Considering the fact that 500! gives 1135-digit number no standard, neither integer nor floating, data type is applicable here.

Your task is to write a programs which calculates a factorial of a given number.

Assumptions: Value of a number ``n" which factorial should be calculated of does not exceed 1000 (although 500! is the name of the problem, 500! is a small limit).

Input

Any number of lines, each containing value ``n" for which you should provide value of n!

Output

2 lines for each input case. First should contain value ``n" followed by character `!'. The second should contain calculated value n!.

Sample Input

10
30
50
100

Sample Output

10!
3628800
30!
265252859812191058636308480000000
50!
30414093201713378043612608166064768844377641568960512000000000000
100!
9332621544394415268169923885626670049071596826438162146859296389521759999322991
5608941463976156518286253697920827223758251185210916864000000000000000000000000

Solution

  1. import java.io.BufferedInputStream;
  2. import java.math.BigInteger;
  3. import java.util.Scanner;

  4. public class Main {

  5. static int size = 1001;
  6. static Scanner in = new Scanner(new BufferedInputStream(System.in));
  7. static BigInteger[] bigIntArray = new BigInteger[size];
  8. static int input;

  9. public static void main(String[] args) {

  10. bigIntArray[0] = BigInteger.ONE;
  11. bigIntArray[1] = BigInteger.ONE;
  12. for (int i=2; i<size; i++) {
  13. bigIntArray[i] = bigIntArray[i-1].multiply(new BigInteger(String.
  14. valueOf(i)));
  15. }

  16. while (in.hasNext()) {
  17. input = in.nextInt();
  18. System.out.println(input+"!");
  19. System.out.println(bigIntArray[input]);
  20. }
  21. }
  22. }
關鍵字:UVa Online Judge, ACM. Java


留言

相關文章

這個網誌中的熱門文章

【玉山U Bear卡】2019必辦神卡!網購5%超商8%電影20%!

【證件遺失補辦】身分證/健保卡/駕照/行照/信用卡總整理

【玉山卡攻略】神卡雲集,依序辦卡拿新舊戶首刷最佳解!

【地價稅繳納攻略】2019/108年度信用卡回饋/分期整理!

【信用卡排行榜】最新現金回饋信用卡排行榜(含2020可用)!

【博客來折價券】博客來免費序號e-coupon分享(持續更新)

【全家FamiPay】新光寰宇10%/玉山UBear 8%/台新Fish 8%!

【永豐大戶DAWHO】現金回饋信用卡國內2%國外3%最高8%!

【聯邦+LINE一卡通2%】不必自己算!繳費條碼產生器上線!

【統一發票】獎金儲值加碼,7-11送中熱美/全聯享贈點!