Competitive/Collaborative Programming Class

ICPC Computer Programming Contest Prep

Problem Solving in Computer Science

Spring 2015 -- CSC 2700 Section 01

[Isaac's Home Page ]  [Mailing List ]  [Class Page ]  [Normal ]  

week02/f/NumericallySpeaking.rtf

{\rtf1\ansi\ansicpg1252\cocoartf949\cocoasubrtf540
{\fonttbl\f0\fmodern\fcharset0 Courier;\f1\fswiss\fcharset0 ArialMT;}
{\colortbl;\red255\green255\blue255;\red34\green34\blue34;\red255\green255\blue255;\red102\green102\blue102;
\red249\green249\blue249;}
\margl1440\margr1440\vieww9000\viewh8400\viewkind0
\deftab720
\pard\pardeftab720\sl360\ql\qnatural

\f0\fs26 \cf2 \cb3 /*\
  UVa 619 - Numerically Speaking\
  Randall Head \
*/\
\
import java.util.Scanner;\
import java.math.BigInteger;\
\
class Main \{\
  static final BigInteger TWENTY_SIX = BigInteger.valueOf(26);\
\
  public static void main(String[] args) \{\
    Scanner sc = new Scanner(System.in);\
    while (true) \{\
      if (sc.hasNextBigInteger()) \{\
        BigInteger n = sc.nextBigInteger();\
        String s = convertToString(n);\
        System.out.printf("%-22s%s%n", s, getNumberToBePrinted(n));\
      \}\
      else if (sc.hasNext()) \{\
        String s = sc.next();\
        if (s.equals("*"))\
          break;\
        BigInteger n = convertToNumber(s);\
        System.out.printf("%-22s%s%n", s, getNumberToBePrinted(n));\
      \}\
    \}\
  \}\
\
  static String convertToString(BigInteger n) \{\
    StringBuilder sb = new StringBuilder();\
    do \{\
      sb.append((char)('a' +\
        n.subtract(BigInteger.ONE).mod(TWENTY_SIX).intValue()));\
      n = n.subtract(BigInteger.ONE).divide(TWENTY_SIX);\
    \} while (n.compareTo(BigInteger.ZERO) != 0);\
    sb.reverse();\
    return sb.toString();\
  \}\
\
  static BigInteger convertToNumber(String s) \{\
    BigInteger n = BigInteger.ZERO;\
    for (int i = 0; i < s.length(); i++) \{\
      n = n.multiply(TWENTY_SIX);\
      n = n.add(BigInteger.valueOf((int)(s.charAt(i) - 'a' + 1)));\
    \}\
    return n;\
  \}\
\
  static String getNumberToBePrinted(BigInteger n) \{\
    String s = n.toString();\
    StringBuilder sb = new StringBuilder();\
    for (int i = s.length() - 1, j = 0; i >= 0; i--, j++) \{\
      sb.append(s.charAt(i));\
      if (i > 0 && (j % 3) == 2)\
        sb.append(',');\
    \}\
    sb.reverse();\
    return sb.toString();\
  \}\
\}\
\
\pard\pardeftab720\sl360\ql\qnatural

\f1 \cf2 \
\pard\pardeftab720\sl340\ql\qnatural

\fs22 \cf4 \cb5 \
}

The statements and opinions included in these pages are those of only. Any statements and opinions included in these pages are not those of Louisiana State University or the LSU Board of Supervisors.
© 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015