#include <stdio.h>#define MAX_SIZE 10int main() { int set1[MAX_SIZE], set2[MAX_SIZE], union_set[MAX_SIZE], intersection_set[MAX_SIZE], complement_set[MAX_SIZE]; int n1, n2, i, j, k, flag; // 输入第一个集合 printf("Enter the size of set 1: "); scanf("%d", &n1); printf("Enter the elements of set 1: "); for (i = 0; i < n1; i++) { scanf("%d", &set1[i]); } // 输入第二个集合 printf("Enter the size of set 2: "); scanf("%d", &n2); printf("Enter the elements of set 2: "); for (i = 0; i < n2; i++) { scanf("%d", &set2[i]); } // 计算并集 k = 0; for (i = 0; i < n1; i++) { union_set[k++] = set1[i]; } for (i = 0; i < n2; i++) { flag = 1; for (j = 0; j < n1; j++) { if (set2[i] == set1[j]) { flag = 0; break; } } if (flag) { union_set[k++] = set2[i]; } } printf("The union of set 1 and set 2 is: "); for (i = 0; i < k; i++) { printf("%d ", union_set[i]); } printf("\n"); // 计算交集 k = 0; for (i = 0; i < n1; i++) { for (j = 0; j < n2; j++) { if (set1[i] == set2[j]) { intersection_set[k++] = set1[i]; } } } printf("The intersection of set 1 and set 2 is: "); for (i = 0; i < k; i++) { printf("%d ", intersection_set[i]); } printf("\n"); // 计算补集 k = 0; for (i = 0; i < n1; i++) { flag = 1; for (j = 0; j < n2; j++) { if (set1[i] == set2[j]) { flag = 0; break; } } if (flag) { complement_set[k++] = set1[i]; } } printf("The complement of set 1 with respect to set 2 is: "); for (i = 0; i < k; i++) { printf("%d ", complement_set[i]); } printf("\n"); return 0;}解释一下这个程序
时间: 2024-04-04 15:30:40 浏览: 73
#include <stdio.h>
这是一个 C 语言程序,用于计算两个集合的并集、交集和补集。程序首先定义了三个数组,用于存储并集、交集和补集。然后程序要求用户输入两个集合的元素,分别存储在 set1 和 set2 数组中。程序使用 for 循环计算并集,并将结果存储在 union_set 数组中。计算交集的方式也是使用 for 循环,将结果存储在 intersection_set 数组中。最后,程序计算 set1 相对于 set2 的补集,并将结果存储在 complement_set 数组中。程序最后输出计算结果。
阅读全文