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
5
using namespace
std;
6
14
int
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
26
class
Solution
27
{
28
public
:
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
52
int
main
()
53
{
54
cout <<
Solution
().
guessNumber
(10) << endl;
55
return
0;
56
}
Solution
Definition
main.cpp:27
Solution::guessNumber
int guessNumber(int n)
Definition
main.cpp:29
guess
int guess(int num)
Guess API.
Definition
main.cpp:14
main
int main()
Definition
main.cpp:52
main.cpp
Generated by
1.9.8