Guess the Number

An interactive problem

I'm thinking of a number between 1 and 2×109, inclusive.
Now it will be your job to try to find my number!

To guess a number, just print it out. See the wiki for instructions on flushing output in your language; you must do this after every guess you print.

I will output something back: "Higher" if your number is too low, or "Lower" if your number is too high.
I will output "OK" if you guessed correctly! Your program should stop at this point.

Your guessing algorithm must be optimal—you must take less than 32 guesses.

Sample session

You: 10
Me: Lower
You: 8
Me: Higher
You: 9
Me: OK

All Submissions
Best Solutions

Point Value: 5
Time Limit: 1.00s
Memory Limit: 16M
Added: Jan 18, 2009

Languages Allowed:

Comments (Search)


Don't put readln, right after your flush(output).
Otherwise, the interactive program's output just gets dumped.

Can someone tell me what my problem is for the last test case?
I pretty sure its not the size of my numbers, but maybe its cause my codes somewhat inefficient?

I skimmed your code and there isn't much wrong with it.

Instead of using b to control ceil/floor, try saving the previous guess.

Yeah, a major problem associated with binary search is figuring out when to take the ceil and when to take the floor. Right now you seem to be alternating, which isn't great - try experimenting a bit. You may even have to use a different formula when you go up as opposed to when you go down, I won't tell you exactly what.

what does it mean by "flush(output)" on pascal?

After writeln(...); you should have flush(output);
Sometimes it's necessary, sometimes it's not - I don't really know.

Anyway, your program doesn't seem to need it.
The problem is that your numbers aren't big enough (it goes up to two billion)
Also, the judge waits for your program to finish because it readln;s at the end - so you have all TLEs (even though you still get 40/50)