Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create Josephusproblem #185

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions Josephusproblem
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#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;
}

index = ((index + k) % person.size());

person.erase(person.begin() + index);

Josh(person, k, index);
}

int main()
{
int n = 14;
// josephus problem
int k = 2;
k--; // (k-1)th person will be killed
int index
= 0; // The index where the person which will die

vector<int> person;
// fill the person vector
for (int i = 1; i <= n; i++) {
person.push_back(i);
}

Josh(person, k, index);
}