Pages

Sunday 9 September 2012

UVA - 623-500!

import java.io.*;
import java.math.BigInteger;
import java.util.IllegalFormatException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;


public class Main{

    public static void main(String[] args) throws FileNotFoundException, IOException {
            TextIO.readStream(System.in);
            BigInteger arr[]=new BigInteger[1001];
                for(int i=0;i<1001;i++){
                    arr[i]= BigInteger.ZERO;
                }
            while (true){
                int x=0;
                try{
                x=TextIO.getInt();
                }catch(Exception e){
                    break;
                }
                System.out.println(x+"!");
                System.out.println(fact(x,arr));
            }
    }
   
    static BigInteger fact(int x,BigInteger[] arr){
        if(x==1 || x==0)
             return BigInteger.ONE;
        if(arr[x]!=BigInteger.ZERO){
            return arr[x];
        }
        BigInteger temp=BigInteger.ONE;
        temp=BigInteger.valueOf(x).multiply(fact(x-1,arr));
        return temp;
    }
}

No comments:

Post a Comment