-
Notifications
You must be signed in to change notification settings - Fork 7
/
bestguess.cpp
72 lines (60 loc) · 1.56 KB
/
bestguess.cpp
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
#include <iostream>
#include <vector>
#include <cstdlib>
#include <string>
#include <algorithm>
int middle(int a, int b)
{
return (a + b) / 2;
}
int main(int argc, char *argv[])
{
int min, max;
if (argc >= 3) {
min = std::atoi(argv[1]);
max = std::atoi(argv[2]);
} else {
std::cout << "Lower bound: ";
std::cin >> min;
std::cout << "Upper bound: ";
std::cin >> max;
}
if (min == max) {
std::cout << "I don't know; " << min << " is as good a guess as any." << std::endl;
return 0;
} else if (min > max) {
std::swap(min, max);
}
std::cout << "Enter other guesses ('stop' when done):" << std::endl;
std::vector<int> otherGuesses;
bool stop = false;
while (!stop) {
std::string input;
std::cin >> input;
if (input == "stop") {
stop = true;
} else {
int guess = std::atoi(input.c_str());
if (min <= guess && guess <= max)
otherGuesses.push_back(guess);
else
std::cout << guess << " is out of range; ignoring" << std::endl;
}
}
otherGuesses.push_back(min);
otherGuesses.push_back(max);
std::sort(otherGuesses.begin(), otherGuesses.end());
std::size_t count = otherGuesses.size();
std::size_t guessBeforeBest = 0;
unsigned int bestDifference = 0;
for (std::size_t i = 0; i < count - 1; i++) {
unsigned int diff = otherGuesses[i+1] - otherGuesses[i];
if (diff > bestDifference) {
guessBeforeBest = i;
bestDifference = diff;
}
}
int bestGuess = middle(otherGuesses[guessBeforeBest], otherGuesses[guessBeforeBest + 1]);
std::cout << "The best number to guess is " << bestGuess << "." << std::endl;
return 0;
}