Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- int array_include(int *a, int n, int *b, int m);
- /* Ф-ия рассматривает целочисленные массивы как числовые множества без повторений.
- * Если a включено в b, то возвращает 1. иначе 0.
- */
- int array_include(int *a, int n, int *b, int m)
- {
- int i, j, check_include, check_iteration;
- for(i = 0; i<n; i++)
- {
- check_iteration = 0;
- // Если a[i] уже встречался ранее, его не проверяю
- for(j = 0; j<i; j++)
- if(a[i]==a[j])
- {
- check_iteration = 1;
- break;
- }
- if(check_iteration)// Элемент уже встречался
- continue;
- check_include = 0;
- for(j = 0; j<m; j++)
- {
- if(a[i]==b[i])
- {
- check_include = 1;// Элемент a[i] есть в b
- break;
- }
- }
- if(!check_include)
- return 0;
- }
- return 1;
- }
- int main(void)
- {
- FILE *f;
- int *a, *b;
- int i, n, m;
- if((f = fopen("input.txt", "r"))==NULL)
- {
- printf("Open file error!\n");
- return 1;
- }
- if(fscanf(f, "%d", &n)<=0)// Считываю кол-во элементов в 1ом массиве
- {
- printf("Input from file error!\n");
- return 2;
- }
- a = (int*)malloc(n*sizeof(int));
- for(i = 0; i<n; i++)
- {
- if(fscanf(f, "%d", &a[i])<=0)
- {
- printf("Input from file error!\n");
- free(a);
- return 2;
- }
- }
- if(fscanf(f, "%d", &m)<=0)// Считываю кол-во элементов во 2ом массиве
- {
- printf("Input from file error!\n");
- free(a);
- return 2;
- }
- b = (int*)malloc(m*sizeof(int));
- for(i = 0; i<m; i++)
- {
- if(fscanf(f, "%d", &b[i])<=0)
- {
- printf("Input from file error!\n");
- free(a);
- free(b);
- return 2;
- }
- }
- // Массивы считаны
- // Печать массивов(пригодится при отладке)
- /*
- for(int i = 0; i<n; i++)
- printf("a[%d]=%d\t", i, a[i]);
- printf("\n");
- for(int i = 0; i<m; i++)
- printf("b[%d]=%d\t", i, b[i]);
- printf("\n");
- */
- if(array_include(a, n, b, m))
- {
- if(array_include(b, m, a, n)) printf("A=B\n");
- else printf("A included in B!\n");
- free(a);
- free(b);
- return 0;
- }
- free(a);
- free(b);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement