Skip to content
Merged
Changes from 3 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
47 changes: 47 additions & 0 deletions archive/c/c-plus-plus/josephus-problem.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#include <iostream>
#include <stdexcept>
#include <vector>
using namespace std;

void josephus(int n, int k) {
vector<int> people;
for (int i = 1; i <= n; ++i) {
people.push_back(i);
}

int index = 0;
while (people.size() > 1) {
cout << "[" << people[0];
for (size_t i = 1; i < people.size(); ++i) {
cout << ", " << people[i];
}
cout << "] " << (k - 1) << " " << index << endl;
index = (index + k - 1) % people.size();
people.erase(people.begin() + index);
if (index == people.size()) index = 0;
}
cout << people[0] << endl;
}

int main(int argc, char* argv[]) {
if (argc != 3) {
cerr << "Usage: please input the total number of people and number of people to skip.\n";
return 1;
}
try {
int n = stoi(argv[1]);
int k = stoi(argv[2]);
if (n <= 0 || k <= 0) {
cerr << "Usage: please input the total number of people and number of people to skip.\n";
return 1;
}
josephus(n, k);
return 0;
} catch (const invalid_argument&) {
cerr << "Usage: please input the total number of people and number of people to skip.\n";
return 1;
} catch (const out_of_range&) {
cerr << "Usage: please input the total number of people and number of people to skip.\n";
return 1;
}
}
Loading