I finally got TO getting a WA, after multiple REs... I guess the judge doesn't like packages.
Anyway, here's my code that gets WA:
Code: Select all
import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.StringTokenizer;
class Main {
/**
* @param args
*/
public static void main(String[] args)
{
Main myWork = new Main();
myWork.begin(args);
}
void begin(String[] args)
{
ArrayList<long[]> numbers = null;
if (args.length > 0)
{
numbers = new ArrayList<long[]>();
numbers.add(parseLongs(args[0]));
}
else
numbers = getInput();
long displayInt = 0;
for ( int i = 0; i < numbers.size(); i ++)
{
displayInt = countCycles(numbers.get(i));
System.out.println(numbers.get(i)[0] + " " + numbers.get(i)[1] + " " + displayInt);
}
}
private static ArrayList<long[]> getInput(String[] input)
{
String currentLine = null;
ArrayList<long[]> numberPairs = new ArrayList<long[]>();
// while ((currentLine = Main.ReadLn (255)) != null && currentLine.length() > 1)
// {
// numberPairs.add(parseLongs(currentLine));
// }
while ((currentLine = getLine("")) != null)
{
numberPairs.add(parseLongs(currentLine));
}
return numberPairs;
}
private static ArrayList<long[]> getInput()
{
String[] input = new String[0];
return getInput(input);
}
private static long[] parseLongs(String input)
{
StringTokenizer tokenizer = new StringTokenizer(input);
long num1 = Long.parseLong(tokenizer.nextToken());
long num2 = Long.parseLong(tokenizer.nextToken());
long returnArray[] = new long[2];
returnArray[0] = num1;
returnArray[1] = num2;
return returnArray;
}
private static int countCycles(long[] input)
{
int currentCycles = 0;
int maxCycles = 0;
long[] bounds = new long[2];
for (int i = 0; i < input.length; i++)
{
bounds[i] = input[i];
}
// Make sure bounds are sorted:
Arrays.sort(bounds);
// start with lower bound, loop until upper bound:
for (long i = bounds[0]; i <= bounds[1]; i++)
{
long result = i;
currentCycles = 1;
while (result != 1)
{
if ((result & 1) > 0)
{
result = 3 * result + 1;
}
else
{
result = result / 2;
}
currentCycles++;
}
if (currentCycles > maxCycles)
{
maxCycles = currentCycles;
}
}
return maxCycles;
}
private static String getLine (String prompt)
{
System.out.print (prompt);
String result = new String();
System.out.flush();
try
{
BufferedReader in = new BufferedReader (new InputStreamReader(System.in));
result = in.readLine();
if (result == null || result.equals(""))
return null;
}
catch (IOException ex)
{
System.err.println("An IOException ocurred: ");
}
return result;
}
private static String ReadLn (int maxLg) // utility function to read from stdin
{
byte lin[] = new byte [maxLg];
int lg = 0, car = -1;
String line = "";
try
{
while (lg < maxLg)
{
car = System.in.read();
if ((car < 0) || (car == '\n'))
{
car = -1;
break;
}
lin [lg++] += car;
}
}
catch (IOException e)
{
return (null);
}
if ((car < 0) && (lg == 0)) return (null); // eof
return (new String (lin, 0, lg));
}
private static long[] parseInts(String input)
{
long[] returnLong = new long[2];
ArrayList<String> tempArrayList = new ArrayList<String>();
String[] tempArray = null;
tempArray = input.split(" ");
if (tempArray.length > 0)
{
for (int i = 0; i < tempArray.length; i++)
{
if (tempArray[i].length() > 0)
tempArrayList.add((String) tempArray[i]);
}
}
tempArray = (String[]) tempArrayList.toArray(new String[tempArrayList.size()]);
if (tempArray.length != 2)
return null;
for ( int i = 0; i < 2; i++)
{
try
{
returnLong[i] = Long.parseLong(tempArray[i]);
}
catch (NumberFormatException e)
{
System.out.println("Error processing input! Not a valid long!");
}
}
return returnLong;
}
}
I have tried, on my build:
extraneous spaces, before and after and in between a set of numbers
boundary conditions (1 1000000 works)
out of order values (i.e. 50 10)
not outputting anything extra, as far as i can tell