1 / 5
Feb 2005

Im getting a time exceed limit for programs with one second although my algorithm is very good. The problem is reading strings when all i need is numbers, so i have to parce in a loop several input from the tokenizer... how can i make it faster then???? this is fustrating!!! when C++ users can solve the same in 0.00s!!!

ok as Kawigi suggested i used BufferedInputStream but now i have a problem, if i have an input of 6 ints separated by space... is there anyway i can read them more efficiently than read them as a string, use StringTokenizer and then parse them to int???? cuz that's inside the loop, and that takes long time.. i only need the numbers and not the string.. how can i achieve this???

smartmalk

OK OK OK me again im using BufferedInputStream as u suggested:

BufferedInputStream line = new BufferedInputStream(System.in);
int number = line.read();

and it's not reading the number quite well, how come?
if i enter 1 I receive a 49! or a 2 and I receive 53!!! i mean... what's going on???

smartmalk

Problem: Reading raw input using a BufferedInputStream is done one byte at a time. So line.read() returns an int type, but it doesn't actually read an integer. Here's roughly the way I do it:

BufferedInputStream in = new BufferedInputStream(System.in, BUFFER_SIZE); //buffer size is usually between 1000000 and 5000000, I tweak it to see what makes it the fastest
int num = 0;
int read;
while ((read = in.read()-'0') >= 0)
	num = num*10 + read;

This will read the number and one extra character (possibly a newline or a space) for you. Do that 6 times for your six numbers, and the line should be covered.

The reason you were getting 49 for 1 is that '1' is ascii 49 smile

8 months later

astonished) Well thats exactly what i thought.laughing:lol:

Suggested Topics

Topic Category Replies Views Activity
Off-topic 2 301 Jun '24

Want to read more? Browse other topics in Off-topic or view latest topics.