Advertisement
Bisus

Untitled

Oct 6th, 2019
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.41 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[j])
  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.     if((a = (int*)malloc(n*sizeof(int)))==NULL)
  60.     {
  61.         printf("Failed to get memory!\n");
  62.         return 3;
  63.     }
  64.     for(i = 0; i<n; i++)
  65.     {
  66.         if(fscanf(f, "%d", &a[i])<=0)
  67.         {
  68.             printf("Input from file error!\n");
  69.             free(a);
  70.             return 2;
  71.         }
  72.     }
  73.  
  74.     if(fscanf(f, "%d", &m)<=0)// Считываю кол-во элементов во 2ом массиве
  75.     {
  76.         printf("Input from file error!\n");
  77.         free(a);
  78.         return 2;
  79.     }
  80.  
  81.     if((b = (int*)malloc(m*sizeof(int)))==NULL) {
  82.         printf("Failed to get memory!\n");
  83.         free(a);
  84.         return 3;
  85.     }
  86.     for(i = 0; i<m; i++)
  87.     {
  88.         if(fscanf(f, "%d", &b[i])<=0)
  89.         {
  90.             printf("Input from file error!\n");
  91.             free(a);
  92.             free(b);
  93.             return 2;
  94.         }
  95.     }
  96.     // Массивы считаны
  97.  
  98.     // Печать массивов(пригодится при отладке)
  99.     /*
  100.     for(int i = 0; i<n; i++)
  101.         printf("a[%d]=%d\t", i, a[i]);
  102.     printf("\n");
  103.     for(int i = 0; i<m; i++)
  104.         printf("b[%d]=%d\t", i, b[i]);
  105.     printf("\n");
  106.     */
  107.  
  108.     if(array_include(a, n, b, m))
  109.     {
  110.         if(array_include(b, m, a, n)) printf("A=B\n");
  111.         else printf("A included in B!\n");
  112.  
  113.         free(a);
  114.         free(b);
  115.         return 0;
  116.     }
  117.  
  118.     free(a);
  119.     free(b);
  120.     return 0;
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement