SHARE
TWEET

Untitled

a guest Jul 12th, 2019 67 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. template<typename RandomIt>
  2. void MakeJosephusPermutation(RandomIt first, RandomIt last, uint32_t step_size)
  3. {
  4.     list<typename RandomIt::value_type> pool;
  5.     move(first, last, back_inserter(pool));
  6.     size_t cur_pos = 0;
  7.   size_t pred_pos = 0;
  8.  
  9.   auto it = pool.begin();
  10.  
  11.     while (!pool.empty())
  12.   {
  13.     for (size_t ind = pred_pos; ind < cur_pos; ++ind)
  14.     {
  15.       it++;
  16.       if (it++ == pool.end()) it = pool.begin();
  17.     }
  18.     *(first++) = move(*it);
  19.     pool.erase(it);
  20.     if (pool.empty()) break;
  21.  
  22.     pred_pos = cur_pos;
  23.     cur_pos = (cur_pos + step_size - 1) % pool.size();
  24.     }
  25.  
  26. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top