Advertisement
CleverCode

BandieraItaliana[C]

May 15th, 2022
968
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.65 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdbool.h>
  3.  
  4. // Global
  5. const int GREEN = 0;
  6. const int WHITE = 1;
  7. const int RED = 2;
  8.  
  9. // All Test
  10. void printArray(int array[], int n);
  11. void swap(int array[], int i, int j);
  12. bool splitColorsCheck(int array[], int n);
  13.  
  14. // First Test
  15. void splitColors(int array[], int n);
  16.  
  17. // Second Test
  18. void splitColors2(int array[], int k);
  19.  
  20.  
  21. int main(int argc, char *argv[]) {
  22.     // First Test
  23.     printf("First Test:\n");
  24.     int first_array[] = {1, 0, 2, 0, 0, 1, 2, 2, 1};
  25.     int n = sizeof(first_array) / sizeof(first_array[0]);
  26.     printf("Bandiera Inserita: ");
  27.     printArray(first_array, n);
  28.     splitColors(first_array, n);
  29.     printf("Bandiera Generata: ");
  30.     printArray(first_array, n);
  31.     printf("Bandiera Ordinata? %s", splitColorsCheck(first_array, n) ? "Si" : "No");
  32.    
  33.     printf("\n\n");
  34.  
  35.     // Second Test
  36.     printf("Second Test:\n");
  37.     int second_array[] = {1, 0, 2, 0, 0, 1, 2, 2, 1, 0, 1, 2};
  38.     int k = sizeof(second_array) / sizeof(second_array[0]);
  39.     printf("Bandiera Inserita: ");
  40.     printArray(second_array, k);
  41.     splitColors2(second_array, k);
  42.     printf("Bandiera Generata: ");
  43.     printArray(second_array, n);
  44.     printf("Bandiera Ordinata? %s", splitColorsCheck(second_array, k) ? "Si" : "No");
  45.     printf("\n");
  46. }
  47.  
  48. void printArray(int array[], int n) {
  49.     int i;
  50.     for (i = 0; i < n; i++) {
  51.         printf("%d ", array[i]);
  52.     }
  53.     printf("\n");
  54. }
  55.  
  56. void swap(int array[], int i, int j) {
  57.     int temp = array[i];
  58.     array[i] = array[j];
  59.     array[j] = temp;
  60. }
  61.  
  62. void splitColors(int array[], int n) {
  63.     int last_green = 0, last_white = 0, last_red = n - 1;
  64.  
  65.     while (last_white <= last_red) {
  66.         if (array[last_white] == WHITE) {
  67.             last_white++;
  68.         } else {
  69.             if (array[last_white] == GREEN) {
  70.                 swap(array, last_white, last_green);
  71.                 last_white++;
  72.                 last_green++;
  73.             } else {
  74.                 swap(array, last_white, last_red);
  75.                 last_red--;
  76.             }
  77.         }
  78.     }
  79. }
  80.  
  81. bool splitColorsCheck(int array[], int n) {
  82.     int color = array[0];
  83.     int how_many_colors = 1;
  84.     int i;
  85.    
  86.     if (color != GREEN) {
  87.         return false;
  88.     } else {
  89.         for (i = 1; i < n; i++) {
  90.             if (array[i] != color) {
  91.                 color = array[i];
  92.                 how_many_colors++;
  93.                 i++;   
  94.             }
  95.         }
  96.     }
  97.    
  98.     return how_many_colors == 3 ? true : false;
  99. }
  100.  
  101. void splitColors2(int array[], int k) {
  102.     int last_green = 0, last_white = k - 1, last_red = k - 1;
  103.  
  104.     while (last_green <= last_white) {
  105.         if (array[last_green] == GREEN) {
  106.             last_green++;
  107.         } else {
  108.             if (array[last_green] == WHITE) {
  109.                 swap(array, last_green, last_white);
  110.                 last_white--;
  111.             } else {
  112.                 swap(array, last_red, last_white);
  113.                 swap(array, last_green, last_white);
  114.                 swap(array, last_red, last_white);
  115.                 last_red--;
  116.             }
  117.         }
  118.     }
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement