## Problem in Input Taking

Moderator: Board moderators

### Problem in Input Taking

Hi, Sorry, I am new, so don't blame me for my question if it is stupid.
Here is a problem: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&page=show_problem&problem=36
It hasn't said that it is going to give us the number of inputs at first! so how should I handle the input. I used hasNextInt() but got time limit.

public class Main {

public static void main(String[] args) {
int a,b,x,max=0,count;
Scanner in = new Scanner(System.in);
while(in.hasNextInt())
{
a = in.nextInt();
b = in.nextInt();
max = 0;
count = 1;
for(int i=a;i<=b;i++)
{
x=i;
count = 1;
while(true)
{
if(x == 1)
break;
if(x%2 == 0)
x = x/2;
else
x = 3*x+1;
count++;
}
if(count>max)
max = count;
}
System.out.println(a+" "+b+" "+max);

}
}

}
rezatorabi
New poster

Posts: 3
Joined: Sun Feb 20, 2011 8:07 am

### Re: Problem in Input Taking

Are you sure you are getting TLE?

Your approach for reading input is correct. Look at other threads related to problem #100 and you will find the reason for getting WA.

sohel
Guru

Posts: 862
Joined: Thu Jan 30, 2003 5:50 am
Location: University of Texas at San Antonio

### Re: Problem in Input Taking

Thanks sohel
You are right. It is WA.
But I can't understand what is wrong. Would you please give me the test case which you say it gives wrong answer.
It's is the final code:

import java.util.Scanner;

/**
*
* @author Reza
*/
public class Main {

public static void main(String[] args) {
// TODO code application logic here
int a,b,x,max=0,count,j,k;
Scanner in = new Scanner(System.in);
while(in.hasNext())
{
a = in.nextInt();
b = in.nextInt();
max = 0;
count = 1;
if(a>b)
{
k=a;
j=b;
}
else
{
k=b;
j=a;
}
for(int i=j;i<=k;i++)
{
x=i;
count = 1;
while(true)
{
if(x == 1)
break;
if(x%2 == 0)
x = x/2;
else
x = 3*x+1;
count++;
}
if(count>max)
max = count;
}
System.out.print(a+" "+b+" "+max);
}
}
}
rezatorabi
New poster

Posts: 3
Joined: Sun Feb 20, 2011 8:07 am

### Re: Problem in Input Taking

Solved!!

Thanks...
rezatorabi
New poster

Posts: 3
Joined: Sun Feb 20, 2011 8:07 am