-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodeDAA.cpp
More file actions
33 lines (28 loc) · 748 Bytes
/
CodeDAA.cpp
File metadata and controls
33 lines (28 loc) · 748 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
#include <iostream>
#include <vector>
using namespace std; // Function to solve the Josephus Problem
int josephus(int n, int k)
{
if (n == 1)
return 0;
// Recursive step:
int lastOne = (josephus(n - 1, k) + k) % n;
return lastOne;
}
int lastPerson(int n, int k)
{
// the result from the josephus function is 0 index, we add 1 based indexing
return josephus(n, k) + 1;
}
int main()
{
int n, step;
cout << "Enter the number of people in the circle: ";
cin >> n;
cout << "Enter the step size: ";
cin >> step;
// using the decrease-and-conquer algorithm
int lastOne = lastPerson(n, step);
cout << "The last person standing is at position: " << lastOne << endl;
return 0;
}