fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4.  
  5.  
  6. void printVect(const std::vector<int> path) {
  7. for( std::vector<int>::const_iterator i = path.begin(); i != path.end(); ++i) {
  8. std::cout << *i << ' ';
  9. }
  10. std::cout << "\n";
  11. }
  12.  
  13. int moveTo(const int index, const int right) {
  14. const int shift = index >> 1;
  15. return ((index & 1) == 0) ? shift : (right - shift);
  16. }
  17.  
  18. template<class T>
  19. int rotate(std::vector<T>& nums, const int start, std::vector<bool>& seen) {
  20.  
  21. const int right = nums.size() - 1;
  22.  
  23. int index = start;
  24. T buffer = nums[index];
  25. int count = 0;
  26.  
  27. do {
  28. const int npos = moveTo(index, right);
  29. const T tmp = nums[npos];
  30. nums[npos] = buffer;
  31. buffer = tmp;
  32. index = npos;
  33. count++;
  34. seen[npos] = true;
  35. } while (index != start);
  36.  
  37. return count;
  38. }
  39.  
  40. template<class T>
  41. void shuffleEvenOdd(std::vector<T>& nums) {
  42.  
  43. const int sz = nums.size();
  44. if (sz < 3) {
  45. return;
  46. }
  47.  
  48. std::vector<bool> seen = std::vector<bool>(sz, false);
  49.  
  50. int count = 0;
  51. for (int i = 0; i < sz && count < sz; i++) {
  52. if (!seen[i]) {
  53. count += rotate(nums, i, seen);
  54. }
  55. }
  56.  
  57. }
  58.  
  59. void prepareEntries(std::vector<int>& nums) {
  60. std::sort(nums.begin(), nums.end());
  61. }
  62.  
  63. void test( std::vector<int>& input, const std::vector<int>& expected, const std::string & testName)
  64. {
  65. std::cout << "Test case : " << testName << "\n";
  66. std::cout << "Input : ";
  67. printVect(input);
  68.  
  69. prepareEntries(input);
  70. std::cout << "Prepd : ";
  71. printVect(input);
  72.  
  73. shuffleEvenOdd(input);
  74. std::cout << "Output: ";
  75. printVect(input);
  76. std::cout << "Expect: ";
  77. printVect(expected);
  78.  
  79. std::cout << (expected == input ? " correct\n" : " !!!FAILED!!!\n");
  80.  
  81. std::cout<<"\n\n";
  82. }
  83.  
  84. void test1()
  85. {
  86. std::vector<int>tmp= {2,2,2};
  87. const std::vector<int>expected= {2,2,2};
  88. const std::string tstName = " Test 1 ";
  89.  
  90. test(tmp,expected, tstName);
  91. }
  92.  
  93. void test2()
  94. {
  95. std::vector<int>tmp= {2};
  96. const std::vector<int>expected= {2};
  97. const std::string tstName = " Test 2 ";
  98.  
  99. test(tmp,expected, tstName);
  100. }
  101.  
  102. void test3()
  103. {
  104. std::vector<int>tmp= {3,3};
  105. const std::vector<int>expected= {3,3};
  106. const std::string tstName = " Test 3 ";
  107.  
  108. test(tmp,expected, tstName);
  109. }
  110.  
  111. void test4()
  112. {
  113. std::vector<int>tmp= {1,-8,2,0,5};
  114. const std::vector<int>expected= {-8,1,5,2,0};
  115. const std::string tstName = " Test 4 ";
  116.  
  117. test(tmp,expected, tstName);
  118. }
  119.  
  120. void test5()
  121. {
  122. std::vector<int>tmp= {-9,1,-8,2,0,5};
  123. const std::vector<int>expected= {-9,0,2,5,1,-8};
  124. const std::string tstName = " Test 5 ";
  125.  
  126. test(tmp,expected, tstName);
  127. }
  128.  
  129.  
  130. void test6()
  131. {
  132. std::vector<int> tmp = std::vector<int>(100);
  133. std::vector<int> expect = std::vector<int>(100);
  134. for (int i = 0; i < 100; i++) {
  135. tmp[i] = i;
  136. int os = i >> 1;
  137. expect[((i & 1) == 1) ? (99 - os) : os] = i;
  138. }
  139. const std::string name = " Sequentials 5";
  140.  
  141. test(tmp, expect, name);
  142.  
  143. }
  144.  
  145. int main()
  146. {
  147. test1();
  148. test2();
  149. test3();
  150. test4();
  151. test5();
  152. test6();
  153.  
  154. return 0;
  155. }
  156.  
  157.  
Success #stdin #stdout 0s 3236KB
stdin
Standard input is empty
stdout
Test case :  Test 1 
Input : 2 2 2 
Prepd : 2 2 2 
Output: 2 2 2 
Expect: 2 2 2 
 correct


Test case :  Test 2 
Input : 2 
Prepd : 2 
Output: 2 
Expect: 2 
 correct


Test case :  Test 3 
Input : 3 3 
Prepd : 3 3 
Output: 3 3 
Expect: 3 3 
 correct


Test case :  Test 4 
Input : 1 -8 2 0 5 
Prepd : -8 0 1 2 5 
Output: -8 1 5 2 0 
Expect: -8 1 5 2 0 
 correct


Test case :  Test 5 
Input : -9 1 -8 2 0 5 
Prepd : -9 -8 0 1 2 5 
Output: -9 0 2 5 1 -8 
Expect: -9 0 2 5 1 -8 
 correct


Test case :  Sequentials 5
Input : 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 
Prepd : 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 
Output: 0 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98 99 97 95 93 91 89 87 85 83 81 79 77 75 73 71 69 67 65 63 61 59 57 55 53 51 49 47 45 43 41 39 37 35 33 31 29 27 25 23 21 19 17 15 13 11 9 7 5 3 1 
Expect: 0 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98 99 97 95 93 91 89 87 85 83 81 79 77 75 73 71 69 67 65 63 61 59 57 55 53 51 49 47 45 43 41 39 37 35 33 31 29 27 25 23 21 19 17 15 13 11 9 7 5 3 1 
 correct