guess-number-higher-or-lower 1.0.0
Guess Number Higher or Lower
Loading...
Searching...
No Matches
main.cpp
Go to the documentation of this file.
1#include <bits/stdc++.h>
2
3#define N 10
4
5using namespace std;
6
14int guess(int num)
15{
16 // static int random = rand() % N;
17 int random = 6;
18 if (num > random)
19 return -1;
20 else if (num < random)
21 return 1;
22 else
23 return 0;
24}
25
27{
28public:
29 int guessNumber(int n)
30 {
31 int left = 1;
32 int right = n;
33 int mid = left + (right - left) / 2;
34 cout << "guessing with " << mid << endl;
35 int api_response = guess(mid);
36 while (api_response != 0)
37 {
38 if (api_response == 1) // 1 - to small
39 left = mid + 1;
40 else if (api_response == -1) // -1 - to big
41 right = mid - 1;
42 else
43 throw runtime_error("API response error");
44 mid = left + (right - left) / 2;
45 cout << "guessing with " << mid << endl;
46 api_response = guess(mid);
47 }
48 return mid;
49 }
50};
51
52int main()
53{
54 cout << Solution().guessNumber(10) << endl;
55 return 0;
56}
int guessNumber(int n)
Definition main.cpp:29
int guess(int num)
Guess API.
Definition main.cpp:14
int main()
Definition main.cpp:52