From 7d544daa312e4bf63febd89bb570be7302bf4949 Mon Sep 17 00:00:00 2001 From: Apurva Vats Date: Sat, 25 Oct 2025 18:09:58 +0530 Subject: [PATCH 1/4] Add Josephus Problem in C++ (Fixes #3332) --- archive/c/c-plus-plus/josephus-problem.cpp | 27 ++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 archive/c/c-plus-plus/josephus-problem.cpp diff --git a/archive/c/c-plus-plus/josephus-problem.cpp b/archive/c/c-plus-plus/josephus-problem.cpp new file mode 100644 index 000000000..57c86973d --- /dev/null +++ b/archive/c/c-plus-plus/josephus-problem.cpp @@ -0,0 +1,27 @@ +#include +#include +using namespace std; + +int josephus(int n, int k) { + int res = 0; + for (int i = 1; i <= n; i++) + res = (res + k) % i; + return res + 1; +} + +int main(int argc, char* argv[]) { + if (argc != 3) { + cerr << "Usage: " << endl; + return 1; + } + + int n = stoi(argv[1]); + int k = stoi(argv[2]); + if (n <= 0 || k <= 0) { + cerr << "Invalid input" << endl; + return 1; + } + + cout << josephus(n, k) << endl; + return 0; +} From 7c8815a635b73e9738b3c2d51bd883ad65efe797 Mon Sep 17 00:00:00 2001 From: Apurva Vats Date: Sat, 25 Oct 2025 18:27:37 +0530 Subject: [PATCH 2/4] Fix: Simplify output format for Josephus Problem to match test requirements --- archive/c/c-plus-plus/josephus-problem.cpp | 27 +++++++++++++++------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/archive/c/c-plus-plus/josephus-problem.cpp b/archive/c/c-plus-plus/josephus-problem.cpp index 57c86973d..072a00f8c 100644 --- a/archive/c/c-plus-plus/josephus-problem.cpp +++ b/archive/c/c-plus-plus/josephus-problem.cpp @@ -1,11 +1,12 @@ #include -#include +#include using namespace std; int josephus(int n, int k) { int res = 0; - for (int i = 1; i <= n; i++) + for (int i = 1; i <= n; ++i) { res = (res + k) % i; + } return res + 1; } @@ -15,13 +16,23 @@ int main(int argc, char* argv[]) { return 1; } - int n = stoi(argv[1]); - int k = stoi(argv[2]); - if (n <= 0 || k <= 0) { + try { + int n = stoi(argv[1]); + int k = stoi(argv[2]); + + if (n <= 0 || k <= 0) { + cerr << "Invalid input" << endl; + return 1; + } + + cout << josephus(n, k) << endl; + return 0; + + } catch (const invalid_argument&) { + cerr << "Invalid input" << endl; + return 1; + } catch (const out_of_range&) { cerr << "Invalid input" << endl; return 1; } - - cout << josephus(n, k) << endl; - return 0; } From 6cecbdafd45857276698d4a719a2545e9473e838 Mon Sep 17 00:00:00 2001 From: Apurva Vats Date: Sat, 25 Oct 2025 20:35:48 +0530 Subject: [PATCH 3/4] Fix: Output for Josephus Problem to match test requirements --- archive/c/c-plus-plus/josephus-problem.cpp | 37 ++++++++++++++-------- 1 file changed, 23 insertions(+), 14 deletions(-) diff --git a/archive/c/c-plus-plus/josephus-problem.cpp b/archive/c/c-plus-plus/josephus-problem.cpp index 072a00f8c..19be09ecd 100644 --- a/archive/c/c-plus-plus/josephus-problem.cpp +++ b/archive/c/c-plus-plus/josephus-problem.cpp @@ -1,38 +1,47 @@ #include #include +#include using namespace std; -int josephus(int n, int k) { - int res = 0; +void josephus(int n, int k) { + vector people; for (int i = 1; i <= n; ++i) { - res = (res + k) % i; + people.push_back(i); } - return res + 1; + + 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: " << endl; + 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 << "Invalid input" << endl; + cerr << "Usage: please input the total number of people and number of people to skip.\n"; return 1; } - - cout << josephus(n, k) << endl; + josephus(n, k); return 0; - } catch (const invalid_argument&) { - cerr << "Invalid input" << endl; + cerr << "Usage: please input the total number of people and number of people to skip.\n"; return 1; } catch (const out_of_range&) { - cerr << "Invalid input" << endl; + cerr << "Usage: please input the total number of people and number of people to skip.\n"; return 1; } -} +} \ No newline at end of file From 76b9dc5522492aadfd4ec484b41dc5f5ebdcbc34 Mon Sep 17 00:00:00 2001 From: Apurva Vats Date: Sat, 25 Oct 2025 22:30:28 +0530 Subject: [PATCH 4/4] Usage message for Josephus Problem to match test requirements --- archive/c/c-plus-plus/josephus-problem.cpp | 47 +++++++++------------- 1 file changed, 19 insertions(+), 28 deletions(-) diff --git a/archive/c/c-plus-plus/josephus-problem.cpp b/archive/c/c-plus-plus/josephus-problem.cpp index 19be09ecd..609054cea 100644 --- a/archive/c/c-plus-plus/josephus-problem.cpp +++ b/archive/c/c-plus-plus/josephus-problem.cpp @@ -1,47 +1,38 @@ #include +#include #include -#include using namespace std; -void josephus(int n, int k) { - vector 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 josephus(int n, int k) { + if (n == 1) + return 1; + else + return (josephus(n - 1, k) + k - 1) % n + 1; } int main(int argc, char* argv[]) { + const string usage_msg = "Usage: please input the total number of people and number of people to skip.\n"; + if (argc != 3) { - cerr << "Usage: please input the total number of people and number of people to skip.\n"; + cerr << usage_msg; 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"; + cerr << usage_msg; return 1; } - josephus(n, k); + + int result = josephus(n, k); + cout << result << endl; 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"; + + } catch (...) { + cerr << usage_msg; return 1; } -} \ No newline at end of file +}