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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
| #include <iostream>
#include <cstdlib>
#include <ctime>
class GuessingGame {
public:
enum hint_type { TOO_HIGH = -1, CORRECT = 0, TOO_LOW = 1 };
GuessingGame (int lo, int hi)
: myNumber(rand() / (RAND_MAX / (hi - lo + 1)) + lo) {
}
hint_type guess (int n) const {
if (n < myNumber) return TOO_LOW;
if (n > myNumber) return TOO_HIGH;
return CORRECT;
};
private:
int myNumber;
};
namespace guess {
int recursive (GuessingGame &g, int low, int high) {
int myguess = (low + high) / 2;
switch (g.guess(myguess)) {
case GuessingGame::TOO_HIGH: return recursive(g, low, myguess);
case GuessingGame::TOO_LOW: return recursive(g, myguess + 1, high);
default: return myguess;
}
}
int iterative (GuessingGame &g, int low, int high) {
while (true) {
int myguess = (low + high) / 2;
switch (g.guess(myguess)) {
case GuessingGame::TOO_HIGH: high = myguess; break;
case GuessingGame::TOO_LOW: low = myguess + 1; break;
default: return myguess;
}
}
}
}
int main () {
std::srand(std::time(0));
int low = 0, high = 100;
GuessingGame g(low, high);
int n = guess::iterative(g, low, high);
std::cout << "The number is " << n << std::endl;
}
|