If I have the following entry:
2
1
-1 0
-5 -3
2 5
0 0
1
-1 0
0 1
0 0
should read as java?? with a variable BufferedReader and readLine () method and read line by line? or just read a single line in the following format:
2 \ n \ n1 \ n-1 0 \ n-5 -3 \ n2 5 \ n0 0 \ n \ n1 \ n-1 0 \ n0 1 \ n0 0
Problems input
Moderator: Board moderators
-
- Guru
- Posts: 5947
- Joined: Thu Sep 01, 2011 9:09 am
- Location: San Jose, CA, USA
Re: Problems input
Which problem number?
Check input and AC output for thousands of problems on uDebug!
-
- New poster
- Posts: 4
- Joined: Wed Nov 14, 2012 7:15 pm
Re: Problems input
I read only one line?
2 \ n \ n1 \ n-1 0 \ n-5 -3 \ n2 5 \ n0 0 \ n \ n1 \ n-1 0 \ n0 1 \ n0 0
or should I read several lines?
2
1
-1 0
-5 -3
2 5
0 0
1
-1 0
0 1
0 0
2 \ n \ n1 \ n-1 0 \ n-5 -3 \ n2 5 \ n0 0 \ n \ n1 \ n-1 0 \ n0 1 \ n0 0
or should I read several lines?
2
1
-1 0
-5 -3
2 5
0 0
1
-1 0
0 1
0 0
-
- Guru
- Posts: 5947
- Joined: Thu Sep 01, 2011 9:09 am
- Location: San Jose, CA, USA
Re: Problems input
I'm not sure I understand what you're asking. On some problems it is best to parse the input line by line, on others you should read an int at a time.
Check input and AC output for thousands of problems on uDebug!
-
- New poster
- Posts: 4
- Joined: Wed Nov 14, 2012 7:15 pm
Re: Problems input
Thanks, I was able to fix the problem, but now I have a new problem with the entry:
if not tell me which is the termination condition, as I know I'll finish the entry?
if not tell me which is the termination condition, as I know I'll finish the entry?
-
- Guru
- Posts: 5947
- Joined: Thu Sep 01, 2011 9:09 am
- Location: San Jose, CA, USA
Re: Problems input
I can't tell you how to parse the input without a problem description.
Check input and AC output for thousands of problems on uDebug!
Re: Problems input
if you're asking about reading such input terminated by blank lines, that will work even if the last line has NO blank line, just check if the line read is a null string.
sample code:
sample code:
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 ) );
String in;
int n = Integer.parseInt( br.readLine() );
br.readLine(); // ignore first blank line
while( n-- != 0 ){
while( ( in = br.readLine() ) != null ){ // will always work even if EOF
// do stuff here
}
}
}
}