Advertisement
Guest User

Untitled

a guest
Dec 15th, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.95 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int main (void) {
  5.     FILE *fin = fopen ("input.bin", "rb");
  6.     FILE *fout = fopen ("output.bin", "wb");
  7.  
  8.     int x, cur_index = 1, alloc = 2;
  9.     int *a = malloc (2);
  10.  
  11.     while (fread (&x, sizeof (int), 1, fin)) {
  12.         if (cur_index == alloc) {
  13.             alloc <<= 1;
  14.             a = realloc (a, alloc * sizeof (int));
  15.         }
  16.         a[cur_index] = x;
  17.         ++cur_index;
  18.     }
  19.  
  20.     int flag1 = 0;
  21.  
  22.     if (cur_index == 2) {
  23.         x = 0;
  24.         fwrite (&x, sizeof (int), 1, fout);
  25.         return 0;
  26.     }
  27.  
  28.     for (int i = 1; i < cur_index; ++i) {
  29.         if (i * 2 < cur_index) {
  30.             if (a[i] <= a[i * 2]) {
  31.                 flag1 = 1;
  32.             } else {
  33.                 flag1 = 0;
  34.                 break;
  35.             }
  36.         }
  37.         if (i * 2 + 1 < cur_index) {
  38.             if (a[i] <= a[i * 2 + 1]) {
  39.                 flag1 = 1;
  40.             } else {
  41.                 flag1 = 0;
  42.                 break;
  43.             }
  44.         }
  45.     }
  46.  
  47.     if (flag1) {
  48.         x = 1;
  49.         fwrite (&x, sizeof (int), 1, fout);
  50.         free (a);
  51.         fclose (fin);
  52.         fclose (fout);
  53.         return 0;
  54.     }
  55.  
  56.     for (int i = 1; i < cur_index; ++i) {
  57.         if (i * 2 < cur_index) {
  58.             if (a[i] > a[i * 2]) {
  59.                 flag1 = 1;
  60.             } else {
  61.                 flag1 = 0;
  62.                 break;
  63.             }
  64.         }
  65.         if (i * 2 + 1 < cur_index) {
  66.             if (a[i] > a[i * 2 + 1]) {
  67.                 flag1 = 1;
  68.             } else {
  69.                 flag1 = 0;
  70.                 break;
  71.             }
  72.         }
  73.     }
  74.  
  75.     if (flag1) {
  76.         x = -1;
  77.         fwrite (&x, sizeof (int), 1, fout);
  78.         free (a);
  79.         fclose (fin);
  80.         fclose (fout);
  81.         return 0;
  82.     } else {
  83.         x = 0;
  84.         fwrite (&x, sizeof (int), 1, fout);
  85.     }
  86.  
  87.     free (a);
  88.     fclose (fin);
  89.     fclose (fout);
  90.     return 0;
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement