daily pastebin goal
4%
SHARE
TWEET

Untitled

kaszperro Mar 13th, 2018 58 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. void fill_tab(int *t, int N) {
  6.     for(int i = 0; i < N; ++i) {
  7.         t[i] = i+1;
  8.     }
  9. }
  10.  
  11. void write(int *t, int N) {
  12.     for(int i =0 ; i < N; ++i) {
  13.         cout << t[i] << " ";
  14.     }
  15.     cout << endl;
  16. }
  17.  
  18. int count_next(int i, int n, int k) {
  19.     return (i+k)%n;
  20. }
  21.  
  22. void move(int *t, int n, int k, int index) {
  23.     int start = index;
  24.     int curr = t[index];
  25.     int next_in = count_next(index, n,k);
  26.  
  27.     while(next_in != start) {
  28.         swap(curr, t[next_in]);
  29.         index = next_in;
  30.         next_in = count_next(index, n, k);
  31.     }
  32.     swap(curr, t[next_in]);
  33. }
  34.  
  35. void move(int *t, int n, int k) {
  36.     if(k==0) return;
  37.     int iter = (n%k == 0)? k : 1;
  38.     for(int i = 0; i < iter; ++i) {
  39.         move(t,n,k,i);
  40.     }
  41. }
  42.  
  43. int main() {
  44.     ios_base::sync_with_stdio(false);
  45.     const int N = 21;
  46.     int t[N];
  47.     fill_tab(t,N);
  48.     write(t,N);
  49.  
  50.     move(t,N,0);
  51.     write(t,N);
  52.     return 0;
  53. }
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
 
Top