Guest User

Untitled

a guest
Oct 18th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.14 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. int getSize() {
  4.     FILE *in = fopen("task.in", "r");
  5.     int size = 0;
  6.    
  7.     for ( char ch; fscanf(in, "%c", &ch) == 1; size++ );
  8.    
  9.     fclose(in);
  10.    
  11.     return size;
  12. }
  13.  
  14. void arraySort(int array[], int size) {
  15.     for ( int i = 1; i < size; i++ ) {
  16.         for ( int j = 1; j < size; j++ ) {
  17.             if ( array[j-1] > array[j] ) {
  18.                 int temp = array[j-1];
  19.                
  20.                 array[j-1] = array[j];
  21.                 array[j] = temp;
  22.             }
  23.         }
  24.     }
  25. }
  26.  
  27. int main() {
  28.     FILE *in = fopen("task.in", "r");
  29.     FILE *out = fopen("task.out", "w");
  30.     int size = getSize();
  31.     int array1[size];
  32.     int array2[size];
  33.     int counter = 0;
  34.     char ch;
  35.    
  36.     for ( int i = 0; i < size && fscanf(in, "%c", &ch) == 1; i++ ) {
  37.         array1[i] = ch - 48;
  38.         array2[i] = ch - 48;
  39.     }
  40.    
  41.     arraySort(array1, size);
  42.    
  43.     for ( int i = 0; i < size; i++ ) {
  44.         if ( array1[i] != array2[i] ) {
  45.             counter += 1;
  46.         }
  47.     }
  48.     fprintf(out, "%d\n", counter/2);
  49.    
  50.     fclose(in);
  51.     fclose(out);
  52.    
  53.     return 0;
  54. }
Add Comment
Please, Sign In to add comment