Write here if you have problems with your Java source code

Moderator: Board moderators

Hello, can somebody tell me why I am getting wrong answer? These are both optimized and brute force codes:

Optimized:
import java.io.*;
import java.util.*;
public class Main{
static long maxSteps=1;
static long[]bigMatrix=new long[1000001];
static StringTokenizer st;
static int veces=0;
public static void main(String[]args)throws IOException{
Scanner scanner=new Scanner(System.in);
for(int i=0;i<1000001;i++) bigMatrix[i]=1;
st=new StringTokenizer(scanner.nextLine());
while(st.hasMoreTokens()){
int a=Integer.parseInt(st.nextToken());
int b=Integer.parseInt(st.nextToken());
for(int i=Math.min(a,b);i<=Math.max(a,b);i++){
long nTemp=calcula(i);
if(nTemp>maxSteps) maxSteps=nTemp;
}
if(veces==0) System.out.print(Math.min(a, b)+" "+Math.max(a, b)+" "+maxSteps);
else System.out.print("\n"+a+" "+b+" "+maxSteps);
maxSteps=1;
st=new StringTokenizer(scanner.nextLine());
}
}

public static long calcula(int number){
long count=1;
int initial=number;
while(number!=1){
if(bigMatrix[initial-1]>1){
count=bigMatrix[initial-1];
break;
} else{
if(number%2==0) number/=2;
else number=3*number + 1;
count++;
}
if(number==1) bigMatrix[initial-1]=count;
}
return count;
}
}

Not Optimized (just change one method):

public static long calcula(int number){
long count=1;
while(number!=1){
if(number%2==0) number/=2;
else number=3*number + 1;
count++;
}
return count;
}
khepani
New poster

Posts: 4
Joined: Tue Nov 10, 2009 9:35 am

I changed this

Code: Select all
`if(veces==0) System.out.print(Math.min(a, b)+" "+Math.max(a, b)+" "+maxSteps);else System.out.print("\n"+a+" "+b+" "+maxSteps);`

for this:

if(veces>0)
System.out.println();
System.out.print(Math.min(a, b)+" "+Math.max(a, b)+" "+maxSteps);
veces=1;

Still not working
khepani
New poster

Posts: 4
Joined: Tue Nov 10, 2009 9:35 am

Solved xD I tought I shouldn't end with a '\n'
khepani
New poster

Posts: 4
Joined: Tue Nov 10, 2009 9:35 am