Skip to content

Commit 51cdc72

Browse files
committed
added optimal stopping code in cpp
1 parent f1fd908 commit 51cdc72

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

C++/optimal_stopping_problem.cpp

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#include <iostream>
2+
#include <time.h>
3+
#define e 2.71828
4+
using namespace std;
5+
6+
int roundNo(float num)
7+
{
8+
return num < 0 ? num - 0.5 : num + 0.5;
9+
}
10+
void printBestCandidate(int candidate[], int n)
11+
{
12+
int sample_size = roundNo(n/e);
13+
cout << "\n\nSample size is " << sample_size << endl;
14+
int best = 0;
15+
for (int i = 1; i < sample_size; i++)
16+
if (candidate[i] > candidate[best])
17+
best = i;
18+
for (int i = sample_size; i < n; i++)
19+
if (candidate[i] >= candidate[best]) {
20+
best = i;
21+
break;
22+
}
23+
24+
if (best >= sample_size)
25+
cout << endl << "Best candidate found is "
26+
<< best + 1 << " with talent "
27+
<< candidate[best] << endl;
28+
else
29+
cout << "Couldn't find a best candidate\n";
30+
}
31+
32+
int main()
33+
{
34+
int n = 8;
35+
int candidate[n];
36+
srand(time(0));
37+
for (int i = 0; i < n; i++)
38+
candidate[i] = 1 + rand() % 8;
39+
cout << "Candidate : ";
40+
for (int i = 0; i < n; i++)
41+
cout << i + 1 << " ";
42+
cout << endl;
43+
cout << " Talents : ";
44+
for (int i = 0; i < n; i++)
45+
cout << candidate[i] << " ";
46+
printBestCandidate(candidate, n);
47+
return 0;
48+
}

0 commit comments

Comments
 (0)