tags:

views:

143

answers:

3

Hi,

I have been beating my head against the wall trying to figure out why this is returning "Wrong Answer." I'd greatly appreciate any feedback.

Edit: I reposted the code, and this version finally fixed the "Runtime Error" by allowing for multiple spaces between the number pair. It now is saying "Wrong Answer" but as far as I can tell, I copied the given algorithm verbatim, so I'm at a loss.

Thanks.

The Problem

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;


public class Main {

public static void main(String[] args) {
   Main mine = new Main();
   mine.begin();
}

public void begin(){
    BufferedReader sys = new BufferedReader(new InputStreamReader(System.in));
    String[] pair;
    try{
        while((pair=sys.readLine().split(" +")).length==2){
            System.out.println(pair[0]+ " " +pair[1] + " " + getMax(Integer.parseInt(pair[0]),Integer.parseInt(pair[1])));
        }
    }catch(IOException ex){
        return;
    }
}

private String getMax(int a, int b){
    int maxcount,thiscount, num, n;

    for(maxcount = -1, num =Math.min(a, b); num <= Math.max(a, b); num++ ){
        for(n = num, thiscount = 1; n!=1; thiscount++){
            if(n%2==0)n=n/2;
            else n = 3*n +1;
        }
        if(thiscount>maxcount) maxcount = thiscount;
    }
    return String.valueOf(maxcount);
}
}
+2  A: 
while(num<4){
...

Is the input always limited to 4 lines?

Lee Reeves
For some reason whenever I don't limit it to a set number it gives "Runtime Error." I don't know why that happens since I can just keep on giving inputs and finally ending with an empty line (like the given solution) and it runs just fine. Any ideas?
James
There is something in their sample data that makes parseInt() blow up. Perhaps it is a line of spaces, or a line that begins with a space. I tired lots of variations but still got either the wrong answer or a runtime error.
UncleO
Have you tried adding `line = line.trim();` before `if(line.length()==0){return;}` and catching NumberFormatException?
Lee Reeves
I updated it to just get the pair immediately, and now it is saying "Wrong Answer".= (better than Runtime Error at least). I can't locate what I am doing wrong and this is driving me crazy.
James
A: 

You might want to reconsider how you parse the lines. I believe the only lines that could have runtime errors in your code are the integer parsing ones. You're assuming that each i and j are separated by a single space. The question makes no mention of how much whitespace will be on a line.

Jeff M
Thanks. That seemed to fix the runtime error problem, but now it is saying "wrong answer." Any ideas why that might be?
James
A: 

If you're getting a wrong answer, and your program works on the samples, then the problem is probably tied to the fact that you're using ints rather then longs.

For the 3n+1 problem, the intermediate values can grow to be larger then an int can handle (2,147,483,647), and it's a common way for judge data to be evil.

CoderTao
Thanks for the idea. I tried using long in place of int and it is still returning that I got the wrong answer. I found a solution on the website (this is the very first problem and shouldn't be difficult after all). It looks like the solution just uses int and does the same thing as my code, but when I submitted his to make sure the site isn't bugged, it says "accepted." It is here http://acm.uva.es/p/data/p100.java.html
James