commit 04af54b28c80829a2702adbbf1e91cb154a9dfed parent 98229838ba23528f01047d769d9a44eac6552469 Author: superpozycja <anna@superpozycja.net> Date: Wed, 30 Oct 2024 23:01:16 +0100 solve josephus problem i Diffstat:
A | sorting_and_searching/josephus_problem_i.cpp | | | 50 | ++++++++++++++++++++++++++++++++++++++++++++++++++ |
1 file changed, 50 insertions(+), 0 deletions(-)
diff --git a/sorting_and_searching/josephus_problem_i.cpp b/sorting_and_searching/josephus_problem_i.cpp @@ -0,0 +1,50 @@ +#include <bits/stdc++.h> + +using namespace std; + +using ui = unsigned int; +using l = long; +using ul = unsigned long; +using ll = long long; +using ull = unsigned long long; + +using vi = vector<int>; +using vui = vector<ui>; +using vl = vector<l>; +using vul = vector<ul>; +using vll = vector<ll>; +using vull = vector<ull>; + +void solve() +{ + int n; + cin >> n; + set<int> res; + for (int i = 1; i <= n; i++) { + res.insert(i); + } + + auto last = res.begin(); + for (int i = 1; i <= n; i++) { + last++; + if (last == res.end()) { + last = res.begin(); + } + cout << *last << " "; + auto l2 = last; + l2++; + res.erase(last); + if (l2 == res.end()) { + l2 = res.begin(); + } + last = l2; + } + cout << "\n"; +} + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + solve(); +}