Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <queue>
- #include <vector>
- using namespace std;
- int main() {
- int n;
- cin >> n;
- queue<int> q;
- vector<int> result;
- for (int i = 1; i <= n; ++i) {
- q.push(i);
- }
- while (q.size() >= 2) {
- // The first person gets injected
- int injected = q.front();
- q.pop();
- result.push_back(injected);
- // The next person escapes to the end
- int escaped = q.front();
- q.pop();
- q.push(escaped);
- }
- // Add the last remaining person
- if (!q.empty()) {
- result.push_back(q.front());
- q.pop();
- }
- // Output the result
- for (size_t i = 0; i < result.size(); ++i) {
- if (i != 0) {
- cout << " ";
- }
- cout << result[i];
- }
- cout << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement