Guest User

Untitled

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