Problem in Input Taking

General topic about Valladolid Online Judge

Moderator: Board moderators

Problem in Input Taking

Postby rezatorabi » Sun Feb 20, 2011 8:21 am

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.

Thanks in Advance for Your Help...

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

Postby sohel » Sun Feb 20, 2011 12:28 pm

Are you sure you are getting TLE?

I added the "java.util.Scanner" header file to your code and it gets wrong answer.
Your approach for reading input is correct. Look at other threads related to problem #100 and you will find the reason for getting WA.
User avatar
sohel
Guru
 
Posts: 862
Joined: Thu Jan 30, 2003 5:50 am
Location: University of Texas at San Antonio

Re: Problem in Input Taking

Postby rezatorabi » Sun Feb 20, 2011 1:57 pm

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

Postby rezatorabi » Sun Feb 20, 2011 3:15 pm

Solved!!

Thanks... :)
rezatorabi
New poster
 
Posts: 3
Joined: Sun Feb 20, 2011 8:07 am


Return to General

Who is online

Users browsing this forum: No registered users and 1 guest