Uva 568 - Just the Facts


 import java.util.*;
import java.math.*;

class Main
{
 public static void main (String[] args)
 {
  Scanner in = new Scanner(System.in);
  BigInteger fact[] = new BigInteger[100006];
  
  BigInteger f=fact[0]=BigInteger.ONE;
  
  for(int i=1;i<=10000;i++)
  {
   fact[i] = f.multiply(BigInteger.valueOf(i));
   f=fact[i];
  }
  
  while(in.hasNext())
  {
   int n = in.nextInt();
   
   //System.out.println(fact[n]);
   BigInteger val=fact[n],lst=BigInteger.ZERO;
   
   while(lst == BigInteger.ZERO)
   {
    lst=val.mod(BigInteger.TEN);
    val=val.divide(BigInteger.TEN);
   }
   
   if(n<10)
   {
    System.out.println("    " + n + " -> " + lst);
   }
   
   else if(n<100)
   {
    System.out.println("   " + n + " -> " + lst);
   }
   
   else if(n<1000)
   {
    System.out.println("  " + n + " -> " + lst);
   }
   
   else if(n<10000)
   {
    System.out.println(" " + n + " -> " + lst);
   }
   
   else
   {
    System.out.println(n + " -> " + lst);
   }
  }
 }
}



Again !!!


 import java.util.*;
import java.math.*;

class Main
{
 public static void main (String[] args)
 {
  Scanner in = new Scanner(System.in);
  BigInteger fact[] = new BigInteger[100006];
  
  BigInteger f=fact[0]=BigInteger.ONE;
  
  for(int i=1;i<=10000;i++)
  {
   fact[i] = f.multiply(BigInteger.valueOf(i));
   f=fact[i];
  }
  
  while(in.hasNext())
  {
   int n = in.nextInt();
   
   //System.out.println(fact[n]);
   BigInteger a=fact[n],b=fact[n],lst=BigInteger.ZERO;
   
   while(a!=BigInteger.ZERO)
   {
    a=a.mod(BigInteger.TEN);
    lst=a;
    
    if(lst!=BigInteger.ZERO)
    {
     break;
    }
    
    b=b.divide(BigInteger.TEN);
    a=b;
   }
   
   if(n<10)
   {
    System.out.println("    " + n + " -> " + lst);
   }
   
   else if(n<100)
   {
    System.out.println("   " + n + " -> " + lst);
   }
   
   else if(n<1000)
   {
    System.out.println("  " + n + " -> " + lst);
   }
   
   else if(n<10000)
   {
    System.out.println(" " + n + " -> " + lst);
   }
   
   else
   {
    System.out.println(n + " -> " + lst);
   }
  }
 }
}

Comments

Popular posts from this blog

SPOJ-CMG - Collecting Mango

LightOJ 1009 - Back to Underworld

LeetCode Palindrome Number