SHARE
TWEET

Untitled

a guest Dec 11th, 2019 82 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 k);
  12.  
  13. int main()
  14. {
  15.   int arr[SIZE];
  16.   int *ptr = arr;
  17.  
  18.   int arr_size = input(ptr);
  19.  
  20.   printf("There is %d series. \n\n", num_of_series(ptr, arr_size));
  21.  
  22.   printf("First and fourth series were swaped. \n");
  23.  
  24.   swap_series(ptr, arr_size, 4);
  25.  
  26.   output(ptr, arr_size);
  27.  
  28.   return 0;
  29. }
  30.  
  31. int input(int *arr)
  32. {
  33.   int n;
  34.   puts("Enter size of array: ");
  35.   scanf("%d", &n);
  36.  
  37.   for(int i = 0; i < n; i++)
  38.   {
  39.     printf("arr[%d] = ", i);
  40.     scanf("%d", &arr[i]);
  41.   }
  42.  
  43.   puts("\n");
  44.  
  45.   return n;
  46. }
  47.  
  48. void output(int *arr, int size)
  49. {
  50.   for(int i = 0; i < size; i++)
  51.   {
  52.     printf("arr[%d] = %d \n", i, arr[i]);
  53.   }
  54.   puts("\n");
  55. }
  56.  
  57. int num_of_series(int *arr, int size)
  58. {
  59.   int num = 1;
  60.  
  61.   for(int i = 1; i < size; i++) if (arr[i] != arr[i - 1]) num++;
  62.  
  63.   return num;
  64. }
  65.  
  66. int delete_k_serie(int *arr, int size, int k)
  67. {
  68.   int num_of_current_serie = 1;
  69.   int size_of_k_serie = 0;
  70.   int end_of_k_serie;
  71.  
  72.   if (num_of_series(arr, size) < k) return size;
  73.  
  74.   for(int i = 1; i < size; i++)
  75.   {
  76.     if (arr[i] != arr[i - 1]) num_of_current_serie += 1;
  77.  
  78.     if (num_of_current_serie == k)
  79.     {
  80.       size_of_k_serie += 1;
  81.       end_of_k_serie = i;
  82.     }
  83.   }
  84.  
  85.   for(int i = end_of_k_serie + 1; i < size; i++) arr[i - size_of_k_serie] = arr[i];
  86.  
  87.   return size - size_of_k_serie;
  88. }
  89.  
  90. void swap_series(int *arr, int size, int k)
  91. {
  92.   if (num_of_series(arr, size) < k) return;
  93.  
  94.   int size_of_first_serie = 1;
  95.  
  96.   while (arr[size_of_first_serie] == arr[size_of_first_serie - 1]) size_of_first_serie += 1;
  97.  
  98.   int num_of_current_serie = 1;
  99.   int size_of_k_serie = 0;
  100.   int end_of_k_serie;
  101.  
  102.   for(int i = 1; i < size; i++)
  103.   {
  104.     if (arr[i] != arr[i - 1]) num_of_current_serie += 1;
  105.  
  106.     if (num_of_current_serie == k)
  107.     {
  108.       size_of_k_serie += 1;
  109.       end_of_k_serie = i;
  110.     }
  111.   }  
  112.  
  113.   if (size_of_first_serie == size_of_k_serie)
  114.   {
  115.     int difference = end_of_k_serie - size_of_first_serie + 1;
  116.     int t;
  117.  
  118.     for(int i = 0; i < size_of_first_serie; i++)
  119.     {
  120.       t = arr[i];
  121.       arr[i] = arr[i + difference];
  122.       arr[i + difference] = t;
  123.     }
  124.   }
  125.   else if (size_of_first_serie < size_of_k_serie)
  126.   {
  127.     int *first_serie = (int*) malloc(sizeof(int) * size_of_first_serie);
  128.  
  129.     for(int i = 0; i < size_of_first_serie; i++) first_serie[i] = arr[i];
  130.  
  131.     // Циклический сдвиг вправо на длину k-ой серии
  132.     for(int i = size + size_of_k_serie; i > size_of_k_serie - 1; i--) arr[i] = arr[i - size_of_k_serie];
  133.  
  134.     // Перенос k-ой серии в начало
  135.     for(int i = 0; i < size_of_k_serie; i++) arr[i] = arr[i + end_of_k_serie + 1];
  136.  
  137.     for(int i = 0; i < size_of_k_serie; i++) arr[i + end_of_k_serie - size_of_k_serie + 1] = first_serie[i];
  138.  
  139.     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];
  140.  
  141.    
  142.  
  143.     for(int i = size_of_k_serie; i < size; i ++) arr[i] = arr[i + size_of_first_serie];
  144.   }
  145. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top