-
Notifications
You must be signed in to change notification settings - Fork 51
/
Josephus.cpp
47 lines (34 loc) · 1.23 KB
/
Josephus.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
//Question 1
// There are N people standing in a circle waiting to be executed. The counting out begins at some point in the circle and proceeds around the circle in a fixed
// direction. In each step, a certain number of people are skipped and the next person is executed. The elimination proceeds around the
// circle (which is becoming smaller and smaller as the executed people are removed), until only the last person remains, who is given freedom.
// Given the total number of persons N and a number k which indicates that k-1 persons are skipped and the kth person is killed in a circle.
// The task is to choose the person in the initial circle that survives.
#include <bits/stdc++.h>
using namespace std;
void Josh(vector<int> person, int k, int index)
{
if (person.size() == 1) {
cout << person[0] << endl;
return;
}
// find the index of first person which will die
index = ((index + k) % person.size());
// remove the first person which is going to be killed
person.erase(person.begin() + index);
Josh(person, k, index);
}
int main()
{
int n = 14;
int k = 2;
k--;
int index
= 0;
vector<int> person;
// fill the person vector
for (int i = 1; i <= n; i++) {
person.push_back(i);
}
Josh(person, k, index);
}