Competitive/Collaborative Programming Class

ICPC Computer Programming Contest Prep

Problem Solving in Computer Science

Fall 2014 -- CSC 2700 Section 01

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

week04/e/Main.java

/* 
 * @author Morgan Hargrove
 * @date 03 Feb. 2013
 * Course: CSC 2700, Sec. 2
 * Instructor: Isaac Traxler
 * UVA Problem 636 - Squares
 */
 
import java.util.*;
import java.io.*;

public class Main
{
    public static void main(String[] args) throws IOException
    {
        final boolean DEBUG = false;
        InputStreamReader isr = new InputStreamReader(System.in);
        BufferedReader br = new BufferedReader(isr);
        ArrayList<Long> pos = new ArrayList<Long>(); //possible squares
        String in = "";
        StringBuffer sb = new StringBuffer("");
        
        //populate pos
        long p = 1;
        while(p * p < 1000000000) //each decimal number is smaller than 1000000000
        {
            pos.add(p * p);
            p++;
        }
        
        while(true)
        {
        //INPUT
            in = br.readLine();
            
            //EOF at 0 (not valid data)
            int n = Integer.parseInt(in);
            if(n == 0)
            {
                break;
            }
            if(DEBUG) //input dump
            {
                System.out.println("  Input: " + in);
            }   
            
        //PROCESS
            //find max digit in line
            char max = '0';
            for(int i = 0; i < in.length(); i++)
            {
                if(in.charAt(i) > max)
                {
                    max = in.charAt(i);
                }
            }
            if(DEBUG) //max found
            {
                System.out.println("    Max: " + max);
            }
           
            //smallest possible base
            int smallest = Integer.parseInt(max + "") + 1;
            if(DEBUG)
            {
                System.out.println("MinBase: " + smallest);
            }
            
            //process
            for(int j = smallest; j < 100; j++)
            {
                long sum = 0;
                for(int k = in.length() - 1, index = 0; k > -1; k--, index++)
                {
                    sum += Integer.parseInt(in.charAt(index) + "") * (Math.pow(j, k));
                }
                if(pos.contains(sum))
                {
                    smallest = j;
                    break;
                }
            }
            sb.append(smallest).append("\n");
        }
        System.out.print(sb);
    }
}

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