-
Notifications
You must be signed in to change notification settings - Fork 2
/
GuessTheNumber.java
42 lines (33 loc) · 1000 Bytes
/
GuessTheNumber.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/*
Problem: https://open.kattis.com/problems/guess
Author: Adrian Reithaug
Submitted: June 6th, 2017
Time: 0.12s / 1.00s
*/
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class GuessTheNumber {
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
int lower = 1;
int upper = 1000;
int answer = 500;
System.out.println(answer);
while (true) {
String result = reader.readLine();
if (result.equals("correct")) {
break;
}
if (result.equals("lower")) {
upper = answer;
answer = (lower + upper) / 2;
}
if (result.equals("higher")) {
lower = answer;
answer = (lower + upper + 1) / 2;
}
System.out.println(answer);
}
}
}