Computer Programming Contest Preparation

ToolBox - Source for: 6/619/Q619.java



/home/toolbox/public_html/solutions/6/619/Q619.java
    1 import java.io.BufferedInputStream;
    2 import java.io.BufferedOutputStream;
    3 import java.io.PrintWriter;
    4 import java.math.BigInteger;
    5 import java.text.DecimalFormat;
    6 import java.util.Scanner;
    7 
    8 
    9 public class Q619
   10 {
   11     static Scanner cin = new Scanner(new BufferedInputStream(System.in));
   12     static PrintWriter cout = new PrintWriter(new BufferedOutputStream(System.out));
   13     public static void main(String [] args)
   14     {
   15         while(cin.hasNext())
   16             {
   17                 String str = cin.next();
   18                 if(str.equals("*"))      break;
   19                 char [] s = str.toCharArray();
   20                 if(s[0] >= '0' && s[0] <= '9')
   21                     Output1(str);
   22                 else
   23                     Output2(str.toCharArray());
   24             }
   25         cout.close();
   26     }
   27     public static void Output1(String str)
   28     {
   29         BigInteger sum = new BigInteger(str);
   30         BigInteger r = new BigInteger("26");
   31         DecimalFormat f= new DecimalFormat("#,###");
   32         char [] s = new char[21];
   33         int top = 0;
   34         while(sum.compareTo(new BigInteger("0")) > 0)
   35             {
   36                 sum = sum.subtract(new BigInteger("1"));
   37                 int a = Integer.valueOf(sum.mod(r).toString());
   38                 sum = sum.divide(r);
   39                 s[top++] = (char) (a+'a');
   40             }
   41         for(int i = top-1; i >= 0; i--)
   42             cout.print(s[i]);
   43         for(int i = 0; i < 23-top-1; i++)
   44             cout.print(" ");
   45         cout.println(f.format(new BigInteger(str)));
   46     }
   47     public static void Output2(char [] s)
   48     {
   49         BigInteger sum = new BigInteger("0");
   50         BigInteger r = new BigInteger("26");
   51         DecimalFormat f= new DecimalFormat("#,###");
   52         for(int i = s.length-1; i >= 0; i--)
   53             {
   54                 BigInteger temp = new BigInteger(Integer.toString(s[i]-'a'+1));
   55                 temp = temp.multiply(r.pow(s.length-1-i));
   56                 sum = sum.add(temp);
   57             }
   58         for(int i = 0; i < s.length; i++)
   59             cout.print(s[i]);
   60         for(int i = 0; i < 23-s.length-1; i++)
   61             cout.print(" ");
   62         cout.println(f.format(sum));
   63     }
   64 }
   65