- Code: Select all
import java.util.*;
import java.io.*;
public class Main{
public static void main (String[]args) throws IOException{
BufferedReader br = new BufferedReader (new InputStreamReader(System.in));
StringBuffer sb = new StringBuffer();
StringTokenizer st = new StringTokenizer (br.readLine());
while (st.hasMoreTokens()){
int a = Integer.parseInt(st.nextToken());
int b = Integer.parseInt(st.nextToken());
if (b<a){
int temp = a;
a = b;
b=temp;
}
int max =0;
for (int i=a; i<=b; i++){
int c = execute(i);
if (c>max){
max=c;
}
}
//System.out.println(a + " " + b + " " + max + "\n");
sb.append(a + " " + b + " " + max + "\n");
st = new StringTokenizer (br.readLine());
}
System.out.print(sb);
System.exit(0);
}
public static int execute (int x){
int count =1;
while (x!=1){
if (x%2==0){
x=x/2;
}
else{
x= 3*x+1;
}
count++;
}
return count;
}
}
And here's the one giving me wrong answer, i changed the looping condition and how I create the tokenizer that's all
- Code: Select all
import java.util.*;
import java.io.*;
public class Main{
public static void main (String[]args) throws IOException{
BufferedReader br = new BufferedReader (new InputStreamReader(System.in));
StringBuffer sb = new StringBuffer();
String line = "";
while ((line=br.readLine())!=null){
StringTokenizer st = new StringTokenizer (line);
int a = Integer.parseInt(st.nextToken());
int b = Integer.parseInt(st.nextToken());
if (b<a){
int temp = a;
a = b;
b=temp;
}
int max =0;
for (int i=a; i<=b; i++){
int c = execute(i);
if (c>max){
max=c;
}
}
//System.out.println(a + " " + b + " " + max + "\n");
sb.append(a + " " + b + " " + max + "\n");
}
System.out.print(sb);
System.exit(0);
}
public static int execute (int x){
int count =1;
while (x!=1){
if (x%2==0){
x=x/2;
}
else{
x= 3*x+1;
}
count++;
}
return count;
}
}
Any help?
