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.
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);
}
}