Advertisement
Bisus

Untitled

Oct 6th, 2019
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.27 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. int array_include(int *a, int n, int *b, int m);
  4.  
  5. /* Ф-ия рассматривает целочисленные массивы как числовые множества без повторений.
  6.  * Если a включено в b, то возвращает 1. иначе 0.
  7.  */
  8. int array_include(int *a, int n, int *b, int m)
  9. {
  10.     int i, j, check_include, check_iteration;
  11.  
  12.     for(i = 0; i<n; i++)
  13.     {
  14.         check_iteration = 0;
  15.         // Если a[i] уже встречался ранее, его не проверяю
  16.         for(j = 0; j<i; j++)
  17.             if(a[i]==a[j])
  18.             {
  19.                 check_iteration = 1;
  20.                 break;
  21.             }
  22.         if(check_iteration)// Элемент уже встречался
  23.             continue;
  24.  
  25.         check_include = 0;
  26.         for(j = 0; j<m; j++)
  27.         {
  28.             if(a[i]==b[i])
  29.             {
  30.                 check_include = 1;// Элемент a[i] есть в b
  31.                 break;
  32.             }
  33.         }
  34.         if(!check_include)
  35.             return 0;
  36.     }
  37.  
  38.     return 1;
  39. }
  40.  
  41. int main(void)
  42. {
  43.     FILE *f;
  44.     int *a, *b;
  45.     int i, n, m;
  46.  
  47.     if((f = fopen("input.txt", "r"))==NULL)
  48.     {
  49.         printf("Open file error!\n");
  50.         return 1;
  51.     }
  52.  
  53.     if(fscanf(f, "%d", &n)<=0)// Считываю кол-во элементов в 1ом массиве
  54.     {
  55.         printf("Input from file error!\n");
  56.         return 2;
  57.     }
  58.  
  59.     a = (int*)malloc(n*sizeof(int));
  60.     for(i = 0; i<n; i++)
  61.     {
  62.         if(fscanf(f, "%d", &a[i])<=0)
  63.         {
  64.             printf("Input from file error!\n");
  65.             free(a);
  66.             return 2;
  67.         }
  68.     }
  69.  
  70.     if(fscanf(f, "%d", &m)<=0)// Считываю кол-во элементов во 2ом массиве
  71.     {
  72.         printf("Input from file error!\n");
  73.         free(a);
  74.         return 2;
  75.     }
  76.  
  77.     b = (int*)malloc(m*sizeof(int));
  78.     for(i = 0; i<m; i++)
  79.     {
  80.         if(fscanf(f, "%d", &b[i])<=0)
  81.         {
  82.             printf("Input from file error!\n");
  83.             free(a);
  84.             free(b);
  85.             return 2;
  86.         }
  87.     }
  88.     // Массивы считаны
  89.  
  90.     // Печать массивов(пригодится при отладке)
  91.     /*
  92.     for(int i = 0; i<n; i++)
  93.         printf("a[%d]=%d\t", i, a[i]);
  94.     printf("\n");
  95.     for(int i = 0; i<m; i++)
  96.         printf("b[%d]=%d\t", i, b[i]);
  97.     printf("\n");
  98.     */
  99.  
  100.     if(array_include(a, n, b, m))
  101.     {
  102.         if(array_include(b, m, a, n)) printf("A=B\n");
  103.         else printf("A included in B!\n");
  104.  
  105.         free(a);
  106.         free(b);
  107.         return 0;
  108.     }
  109.  
  110.     free(a);
  111.     free(b);
  112.     return 0;
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement