首页 > 专题系列 > Java解POJ > POJ 1454 Factorial Frequencies [解题报告] Java
2013
11-09

POJ 1454 Factorial Frequencies [解题报告] Java

Factorial Frequencies

问题描述 :

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 1x2x3x…xn. As she expects to use 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.

输入:

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.

样例输入:

3
8
100
0

样例输出:

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

解题代码:

方法一:

import java.util.*;
import java.math.*;
public class Main{
    public static void main(String args[]) throws Exception {
        Scanner cin=new Scanner(System.in);
        while(true){
            int n=cin.nextInt();
            if(n==0)    break;
            BigInteger ans=BigInteger.valueOf(1);
            for(int i=1;i<=n;i++)
                ans=ans.multiply(BigInteger.valueOf(i));
            char []s=ans.toString().toCharArray();
            int []num=new int[10];
            for(int i=0;i< s.length;i++){
                num[s[i]-'0']++;
            }
            System.out.println(n+"! --");
            System.out.printf("   (0)%5d    (1)%5d    (2)%5d    (3)%5d    (4)%5d\n   (5)%5d    (6)%5d
             (7)%5d    (8)%5d    (9)%5d\n",num[0],num[1],num[2],num[3],num[4],num[5],num[6],num[7],num[8],num[9]);
            
        }
    }
}

方法二:
import java.io.BufferedInputStream;   
import java.math.BigDecimal;   
import java.util.Scanner;   
  
public class Main{   
  
    public static void main(String[] args) {   
        Scanner scan = new Scanner(new BufferedInputStream(System.in));   
        while (scan.hasNext()) {   
            int n = scan.nextInt();   
            if (n == 0) {   
                break;   
            }   
            BigDecimal a = new BigDecimal(n + 1);   
            BigDecimal sum = BigDecimal.ONE;   
            for (BigDecimal i = BigDecimal.ONE; i.compareTo(a) == -1; i = i.add(BigDecimal.ONE)) {   
                sum = sum.multiply(i);   
            }   
            char[] s = sum.toString().toCharArray();   
            int[] num = new int[10];   
            for (int i = 0; i < s.length; i++) {   
                num[s[i] - '0']++;   
            }   
            System.out.println(n + "! --");   
            for (int i = 0; i <= 9; i++) {   
                if (i == 0 | i == 5) {   
                    System.out.print("   " + "(" + i + ")");   
                } else {   
                    System.out.print("    " + "(" + i + ")");   
                }   
                if (num[i] >= 100) {   
                    System.out.print("  " + num[i]);   
                } else if (num[i] >= 10) {   
                    System.out.print("   " + num[i]);   
                } else {   
                    System.out.print("    " + num[i]);   
                }   
                if (i == 4 || i == 9) {   
                    System.out.println("");   
                }   
            }   
        }   
    }   
}

  1. 3,求得所有的为的总和sum—->所有数的总和
    printf( "Not Possible" );—->printf("impossible");
    对吗?