martes, 31 de mayo de 2011

Small Factorial

You are asked to calculate factorials of some small positive integers.
Input


An integer t, 1<=t<=100, denoting the number of testcases, followed by t lines, each containing a single integer n, 1<=n<=100.
Output


For each integer n given at input, display a line with the value of n!

Example
Sample input:
4
1
2
5
3


Sample output:
1
2
120
6

https://www.spoj.pl/problems/FCTRL2/ 




import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.math.BigInteger;

public class SmallFactorial 
{
   public static void main( String[] args ) throws Exception
   {
      BufferedReader br = new BufferedReader( new InputStreamReader( System.in ) );
      BigInteger fact[] = new BigInteger[ 101 ];
  
      fact[ 0 ] = BigInteger.ONE;
      fact[ 1 ] = BigInteger.ONE;
      fact[ 2 ] = new BigInteger( "2" );
      fact[ 3 ] = new BigInteger( "6" );

      int ind = 3;
      int numCasos = Integer.parseInt( br.readLine() );
      int num;
  
      for( int i = 0; i < numCasos; i ++ )
      {
         num = Integer.parseInt( br.readLine() );
   
         while( num > ind )
         {
            fact[ ind + 1 ] = fact[ ind ].multiply( BigInteger.valueOf( ind + 1 ) );
            ind = ind + 1;
         }
   
         System.out.println( fact[ num ].toString() );
      }
   }
}

2 comentarios:

  1. #include
    #define tope 101
    int main(){
    int T,n; double fact[tope];
    scanf("%d",&T);
    // Precalcular
    fact[0] = 1;
    for (int p = 1; p < tope; p++)
    fact[p] = p * fact[p - 1];
    // Precalcular
    for(int i=0; i<T; i++){
    scanf("%d",&n);
    printf("%.0f\n",fact[n]);
    }
    return 0;
    }

    ResponderEliminar
  2. "wrong answer"
    help please
    DAlvarado_UCLA@hotmail.com

    ResponderEliminar