Guest User

Untitled

a guest
Dec 11th, 2019
105
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #define SIZE 1000
  5.  
  6. int input(int *arr);
  7. void output(int *arr, int size);
  8. int num_of_series(int *arr, int size);
  9. int delete_k_serie(int *arr, int size, int k);
  10. void swap_series(int *arr, int size, int k);
  11. int delete_series_lt_k(int *arr, int size, int k);
  12.  
  13. int main()
  14. {
  15. int arr[SIZE];
  16. int *ptr = arr;
  17. int k;
  18.  
  19. int arr_size = input(ptr);
  20.  
  21. printf("There is %d series. \n\n", num_of_series(ptr, arr_size));
  22.  
  23. printf("Enter k: ");
  24. scanf("%d", &k);
  25.  
  26. printf("%d serie was deleted.\n", k);
  27. arr_size = delete_k_serie(ptr, arr_size, k);
  28. output(ptr, arr_size);
  29.  
  30. printf("All series less than %d was deleted.\n", k);
  31. arr_size = delete_series_lt_k(ptr, arr_size, 3);
  32. output(ptr, arr_size);
  33.  
  34. return 0;
  35. }
  36.  
  37. int input(int *arr)
  38. {
  39. int n;
  40. puts("Enter size of array: ");
  41. scanf("%d", &n);
  42.  
  43. for(int i = 0; i < n; i++)
  44. {
  45. printf("arr[%d] = ", i);
  46. scanf("%d", &arr[i]);
  47. }
  48.  
  49. puts("\n");
  50.  
  51. return n;
  52. }
  53.  
  54. void output(int *arr, int size)
  55. {
  56. for(int i = 0; i < size; i++)
  57. {
  58. printf("arr[%d] = %d \n", i, arr[i]);
  59. }
  60. puts("\n");
  61. }
  62.  
  63. int num_of_series(int *arr, int size)
  64. {
  65. int num = 1;
  66.  
  67. for(int i = 1; i < size; i++) if (arr[i] != arr[i - 1]) num++;
  68.  
  69. return num;
  70. }
  71.  
  72. int delete_k_serie(int *arr, int size, int k)
  73. {
  74. int num_of_current_serie = 1;
  75. int size_of_k_serie = 0;
  76. int end_of_k_serie;
  77.  
  78. if (num_of_series(arr, size) < k) return size;
  79.  
  80. for(int i = 1; i < size; i++)
  81. {
  82. if (arr[i] != arr[i - 1]) num_of_current_serie += 1;
  83.  
  84. if (num_of_current_serie == k)
  85. {
  86. size_of_k_serie += 1;
  87. end_of_k_serie = i;
  88. }
  89. }
  90.  
  91. for(int i = end_of_k_serie + 1; i < size; i++) arr[i - size_of_k_serie] = arr[i];
  92.  
  93. return size - size_of_k_serie;
  94. }
  95.  
  96. /*void swap_series(int *arr, int size, int k)
  97. {
  98. if (num_of_series(arr, size) < k) return;
  99.  
  100. int size_of_first_serie = 1;
  101.  
  102. while (arr[size_of_first_serie] == arr[size_of_first_serie - 1]) size_of_first_serie += 1;
  103.  
  104. int num_of_current_serie = 1;
  105. int size_of_k_serie = 0;
  106. int end_of_k_serie;
  107.  
  108. for(int i = 1; i < size; i++)
  109. {
  110. if (arr[i] != arr[i - 1]) num_of_current_serie += 1;
  111.  
  112. if (num_of_current_serie == k)
  113. {
  114. size_of_k_serie += 1;
  115. end_of_k_serie = i;
  116. }
  117. }
  118.  
  119. if (size_of_first_serie == size_of_k_serie)
  120. {
  121. int difference = end_of_k_serie - size_of_first_serie + 1;
  122. int t;
  123.  
  124. for(int i = 0; i < size_of_first_serie; i++)
  125. {
  126. t = arr[i];
  127. arr[i] = arr[i + difference];
  128. arr[i + difference] = t;
  129. }
  130. }
  131. else if (size_of_first_serie < size_of_k_serie)
  132. {
  133. int *first_serie = (int*) malloc(sizeof(int) * size_of_first_serie);
  134.  
  135. for(int i = 0; i < size_of_first_serie; i++) first_serie[i] = arr[i];
  136.  
  137. // Циклический сдвиг вправо на длину k-ой серии
  138. for(int i = size + size_of_k_serie; i > size_of_k_serie - 1; i--) arr[i] = arr[i - size_of_k_serie];
  139.  
  140. // Перенос k-ой серии в начало
  141. for(int i = 0; i < size_of_k_serie; i++) arr[i] = arr[i + end_of_k_serie + 1];
  142.  
  143. for(int i = 0; i < size_of_k_serie; i++) arr[i + end_of_k_serie - size_of_k_serie + 1] = first_serie[i];
  144.  
  145. for(int i = end_of_k_serie - size_of_first_serie + 1; i < size + size_of_first_serie; i++) arr[i] = arr[i - size_of_k_serie + size_of_first_serie];
  146.  
  147. for(int i = size_of_k_serie; i < size; i ++) arr[i] = arr[i + size_of_first_serie];
  148. }
  149. }*/
  150.  
  151. int delete_series_lt_k(int *arr, int size, int k)
  152. {
  153. int len_of_current_serie = 1, start_of_current_serie = 0;
  154.  
  155. int i = 1;
  156.  
  157. while (i < size + 2)
  158. {
  159. if (arr[i] == arr[i - 1])
  160. {
  161. len_of_current_serie++;
  162. i++;
  163. }
  164. else
  165. {
  166. if (len_of_current_serie < k)
  167. {
  168. arr[start_of_current_serie] = 0;
  169.  
  170. int difference = len_of_current_serie - 1;
  171. for(int j = start_of_current_serie + 1; j < size; j++)
  172. {
  173. arr[j] = arr[j + difference];
  174. size -= difference;
  175. }
  176. }
  177.  
  178. len_of_current_serie = 1;
  179. start_of_current_serie = i;
  180. i++;
  181. }
  182. }
  183.  
  184. return size;
  185. }
RAW Paste Data