Tuesday, 5 May 2015

UVA 324 Factorial Frequencies (POJ 1454, UVALive 5432, Regionals 1993 >> North America - North Central NA)

import java.math.BigInteger;
import java.util.Scanner;


public class Main {

 public static void main(String[] args) {
  // TODO Auto-generated method stub
  Scanner sc =  new Scanner(System.in);
  int[] arr = new int[12];
  
  int n;
  
  while(sc.hasNext())
  {
   BigInteger fact = BigInteger.ONE;
   for(int i=0;i<10;i++)
    arr[i]=0;
   n=sc.nextInt();
   if(n==0) break;
   for(int i=2;i<=n;i++)
    fact = fact.multiply(BigInteger.valueOf(i));
   String s = fact.toString();
   int sl = s.length();
   for(int i=0;i<sl;i++)
    arr[ s.charAt(i)-48 ]++;
   System.out.println(n+"! --");
   System.out.printf("   (0)%5d   (1)%5d   (2)%5d   (3)%5d   (4)%5d\n", arr[0], arr[1], arr[2], arr[3], arr[4]);
   System.out.printf("   (5)%5d   (6)%5d   (7)%5d   (8)%5d   (9)%5d\n", arr[5], arr[6], arr[7], arr[8], arr[9]);
   
   
  }
  

 }

}

No comments:

Post a Comment