324 - Factorial Frequencies

UVa網站題目連結
My Solved Problems Performance

In an attempt to bolster her sagging palm-reading business, Madam Phoenix has decided to offer several numerological treats to her customers. She has been able to convince them that the frequency of occurrence of the digits in the decimal representation of factorials bear witness to their futures. Unlike palm-reading, however, she can't just conjure up these frequencies, so she has employed you to determine these values.

Recall that the definition of n! (that is, n factorial) is just tex2html_wrap_inline28 . As she expects to use either the day of the week, the day of the month, or the day of the year as the value of n, you must be able to determine the number of occurrences of each decimal digit in numbers as large as 366 factorial (366!), which has 781 digits.

Input and Output

The input data for the program is simply a list of integers for which the digit counts are desired. All of these input values will be less than or equal to 366 and greater than 0, except for the last integer, which will be zero. Don't bother to process this zero value; just stop your program at that point. The output format isn't too critical, but you should make your program produce results that look similar to those shown below.

Madam Phoenix will be forever (or longer) in your debt; she might even give you a trip if you do your job well!

Sample Input

3
8
100
0

Sample Output

3! --
(0) 0 (1) 0 (2) 0 (3) 0 (4) 0
(5) 0 (6) 1 (7) 0 (8) 0 (9) 0
8! --
(0) 2 (1) 0 (2) 1 (3) 1 (4) 1
(5) 0 (6) 0 (7) 0 (8) 0 (9) 0
100! --
(0) 30 (1) 15 (2) 19 (3) 10 (4) 10
(5) 14 (6) 19 (7) 7 (8) 14 (9) 20

Solution

  1. import java.io.BufferedInputStream;
  2. import java.math.BigInteger;
  3. import java.util.Scanner;
  4.  
  5. public class Main {
  6.  
  7. static Scanner in = new Scanner(new BufferedInputStream(System.in));
  8. static int input, temp;
  9. static int[] intArray = new int[10];
  10. static BigInteger bigInt = new BigInteger("1");
  11. static BigInteger[] bigIntArray = new BigInteger[367];
  12.  
  13. public static void main(String[] args) {
  14. bigIntArray[0] = new BigInteger("0");
  15. bigIntArray[1] = new BigInteger("1");
  16. for (int i=2; i<367; i++) {
  17. bigIntArray[i] = bigIntArray[i-1].multiply(new BigInteger(
  18. String.valueOf(i)));
  19. }
  20.  
  21. while ((input=in.nextInt())!=0) {
  22. process(input);
  23. }
  24. }
  25.  
  26. private static void process(int input) {
  27. for (int i=0; i<intArray.length; i++) {
  28. intArray[i]=0;
  29. }
  30. bigInt = bigIntArray[input];
  31. while (bigInt.compareTo(BigInteger.ZERO)>0) {
  32. temp = bigInt.divideAndRemainder(BigInteger.TEN)[1].intValue();
  33. bigInt = bigInt.divide(BigInteger.TEN);
  34. intArray[temp]++;
  35. }
  36. System.out.println(input+"! --");
  37. System.out.println(" (0) "+intArray[0]+" (1) "+intArray[1]+
  38. " (2) "+intArray[2]+" (3) "+intArray[3]+
  39. " (4) "+intArray[4]);
  40. System.out.println(" (5) "+intArray[5]+" (6) "+intArray[6]+
  41. " (7) "+intArray[7]+" (8) "+intArray[8]+
  42. " (9) "+intArray[9]);
  43. }
  44. }
關鍵字: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送中熱美/全聯享贈點!