-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary_search.cpp
More file actions
61 lines (39 loc) · 787 Bytes
/
binary_search.cpp
File metadata and controls
61 lines (39 loc) · 787 Bytes
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
// GameBS.cpp : Ce fichier contient la fonction 'main'. L'exécution du programme commence et se termine à cet endroit.
//
#include <iostream>
#include <vector>
using namespace std;
#define ll long long int
ll NB = 10;
bool determine(ll m) {
bool ans;
if (NB > m) {
ans = true;
}
else {
ans = false;
}
return ans;
}
ll binary_searchGBS(ll &nb) {
ll ans = 0;
ll low = 0, high = numeric_limits<ll>::max(), mid = 0;
while (low <= high) {
mid = (low + high) / 2;
if (determine(mid)) {
low = mid + 1;
ans = low;
}
else {
high = mid - 1;
}
nb++;
}
return ans;
}
int main()
{
ll nb = 0;
ll ans = binary_searchGBS(nb);
cout << ans << " in " << nb << " hit " << endl;
}