Herowaree

1119

Apr 10th, 2023 (edited)
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.25 KB | None | 0 0
  1. #include <vector>
  2. #include <cstdio>
  3.  
  4. void test_mod() {
  5. unsigned long step = 5;
  6. unsigned long size = 7;
  7. unsigned long index = 4;
  8.  
  9. if (index >= step) {
  10. index -= step;
  11. } else {
  12. index = size - (index + 1);
  13. printf("Index: %lu\n", index);
  14.  
  15. index = (index + step - 1) % size;
  16. printf("Index: %lu\n", index);
  17.  
  18. index = size - (index + 1);
  19. printf("Index: %lu\n", index);
  20. }
  21.  
  22. printf("%lu\n", index);
  23. }
  24.  
  25. void print_vec(std::vector<unsigned long>& vec) {
  26. printf("Vector: ");
  27. for (auto counter = 0; counter < vec.size(); ++counter) {
  28. printf("%lu ", vec[counter]);
  29. }
  30.  
  31. printf("\n");
  32. }
  33.  
  34. int main() {
  35.  
  36. unsigned long N = 10;
  37. unsigned long step_right = 4;
  38. unsigned long step_left = 3;
  39.  
  40. unsigned long index_right = (step_right - 1) % N;
  41. // printf("First right index: %lu\n", index_right);
  42.  
  43. unsigned long index_left = (N - 1) - (step_left - 1);
  44. //printf("First left index: %lu\n", index_left);
  45.  
  46. std::vector<unsigned long> vec;
  47. for (auto counter = 0; counter < N; ++counter) {
  48. vec.push_back(counter + 1);
  49. }
  50.  
  51. // test_mod();
  52.  
  53. bool flag_left = false, flag_right = false;
  54. while (!vec.empty()){
  55. printf("New values - R: %lu / L: %lu / S: %lu\n", index_right, index_left, vec.size());
  56.  
  57. print_vec(vec);
  58.  
  59. if (index_left == 0) flag_left = true;
  60. if (index_right == (vec.size() - 1)) flag_right = true;
  61.  
  62. if (index_right == index_left) {
  63. printf(" %lu,\n", vec[index_right]);
  64. // printf("Both are removing the same value %lu (Index value: %lu - Vector size: %lu)\n", vec[index_right], index_right, vec.size());
  65. vec.erase(vec.begin() + index_right);
  66. --index_left;
  67. } else {
  68. // print_vec(vec);
  69.  
  70. long unsigned out_right, out_left;
  71. if (index_right < index_left) {
  72. // printf(" %lu", vec[index_right]);
  73. // printf("Right movement - Removing value %lu (Index value: %lu - Vector size: %lu)\n", vec[index_right], index_right, vec.size());
  74. out_right = vec[index_right];
  75. vec.erase(vec.begin() + index_right);
  76.  
  77. if (vec.empty()){
  78. printf(" %lu\n", out_right);
  79. break;
  80. }
  81.  
  82. --index_left;
  83. // printf(" %lu", vec[index_left]);
  84. // printf("Left movement - Removing value %lu (Index value: %lu - Vector size: %lu)\n", vec[index_left], index_left, vec.size());
  85. out_left = vec[index_left];
  86. vec.erase(vec.begin() + index_left);
  87. } else {
  88. // printf("Left movement - Removing value %lu (Index value: %lu - Vector size: %lu)\n", vec[index_left], index_left, vec.size());
  89. out_left = vec[index_left];
  90. vec.erase(vec.begin() + index_left);
  91.  
  92. if (vec.empty()){
  93. printf(" %lu\n", out_left);
  94. break;
  95. }
  96.  
  97. --index_right;
  98. --index_left;
  99.  
  100. out_right = vec[index_right];
  101. vec.erase(vec.begin() + index_right);
  102. }
  103.  
  104. printf(" %lu %lu,\n", out_right, out_left);
  105. // print_vec(vec);
  106. }
  107.  
  108. if (flag_left) {
  109. index_left = vec.size() - 1;
  110. flag_left = false;
  111. }
  112.  
  113. if (flag_right) {
  114. index_right = 0;
  115. flag_right = false;
  116. }
  117.  
  118. if (vec.empty()) break;
  119.  
  120. // print_vec(vec);
  121.  
  122. // printf("Previous values - R: %lu / L: %lu / S: %lu\n", index_right, index_left, vec.size());
  123.  
  124. index_right = (index_right + step_right - 1) % vec.size(); // Ok
  125.  
  126. if (index_left >= step_left) {
  127. index_left -= step_left;
  128. } else {
  129. long unsigned tmp = vec.size() - (index_left + 1);
  130. index_left = vec.size() - (((tmp + step_left - 1) % vec.size()) + 1);
  131. }
  132.  
  133. // printf("New values - R: %lu / L: %lu / S: %lu\n", index_right, index_left, vec.size());
  134. }
  135.  
  136. return 0;
  137. }
Advertisement
Add Comment
Please, Sign In to add comment