Advertisement
Derga

Untitled

Oct 4th, 2023
645
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.26 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3.  
  4. using namespace std;
  5.  
  6. int* Compress(int* elements, int elements_count, int k)
  7. {
  8.     int last_pos = 0;
  9.     int new_pos = 0;
  10.     while (last_pos < elements_count)
  11.     {
  12.         if ((last_pos + 1) % k == 0 || elements[last_pos] == 0)
  13.         {
  14.             last_pos++;
  15.             continue;
  16.         }
  17.  
  18.         elements[new_pos] = elements[last_pos];
  19.         new_pos++;
  20.         last_pos++;
  21.     }
  22.     while (new_pos < elements_count)
  23.     {
  24.         elements[new_pos] = 0;
  25.         ++new_pos;
  26.     }
  27.  
  28.     return elements;
  29. }
  30.  
  31. void Print(int* elements, int elements_count) {
  32.     for (int i = 0; i < elements_count; ++i)
  33.     {
  34.         cout << setw(2) << elements[i] << ' ';
  35.     }
  36.     cout << '\n';
  37. }
  38.  
  39. int main()
  40. {
  41.     int k, elements_count;
  42.     cin >> k >> elements_count;
  43.  
  44.     auto elements = new int[elements_count];
  45.     for (int i = 0; i < elements_count; ++i)
  46.     {
  47.         cin >> elements[i];
  48.     }
  49.  
  50.     Print(elements, elements_count);
  51.     elements = Compress(elements, elements_count, k);
  52.     Print(elements, elements_count);
  53.    
  54.     delete[] elements;
  55. }
  56.  
  57. /*
  58. test1
  59. 3
  60. 10
  61. 1 2 3 4 5 6 7 8 9 10
  62.  
  63. 1 2 4 5 7 8 10 0 0 0
  64.  
  65. test2
  66. 2
  67. 10
  68. 1 2 3 4 5 6 7 8 9 10
  69.  
  70. 1 3 5 7 9 0 0 0 0 0
  71.  
  72. test1
  73. 1
  74. 10
  75. 1 2 3 4 5 6 7 8 9 10
  76.  
  77. 0 0 0 0 0 0 0 0 0 0
  78.  
  79. test4
  80. 5
  81. 3
  82. 1 2 3
  83.  
  84. 1 2 3
  85.  
  86. test5
  87. 5
  88. 0
  89.  
  90. test6
  91. 3
  92. 10
  93. 0 1 2 3 4 0 6 7 8 9
  94.  
  95. 1 3 4 6 7 9 0 0 0 0
  96. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement