Advertisement
tepyotin2

Injection

May 1st, 2025
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.88 KB | None | 0 0
  1. #include <iostream>
  2. #include <queue>
  3. #include <vector>
  4.  
  5. using namespace std;
  6.  
  7. int main() {
  8.     int n;
  9.     cin >> n;
  10.    
  11.     queue<int> q;
  12.     vector<int> result;
  13.    
  14.     for (int i = 1; i <= n; ++i) {
  15.         q.push(i);
  16.     }
  17.    
  18.     while (q.size() >= 2) {
  19.         // The first person gets injected
  20.         int injected = q.front();
  21.         q.pop();
  22.         result.push_back(injected);
  23.        
  24.         // The next person escapes to the end
  25.         int escaped = q.front();
  26.         q.pop();
  27.         q.push(escaped);
  28.     }
  29.    
  30.     // Add the last remaining person
  31.     if (!q.empty()) {
  32.         result.push_back(q.front());
  33.         q.pop();
  34.     }
  35.    
  36.     // Output the result
  37.     for (size_t i = 0; i < result.size(); ++i) {
  38.         if (i != 0) {
  39.             cout << " ";
  40.         }
  41.         cout << result[i];
  42.     }
  43.     cout << endl;
  44.    
  45.     return 0;
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement