在C语言中实现两个集合的并集可以通过以下步骤:
以下是一个示例代码实现:
#include <stdio.h>
#define MAX_SIZE 100
void findUnion(int set1[], int set2[], int n1, int n2) {
int unionSet[MAX_SIZE];
int i, j, k = 0;
for (i = 0; i < n1; i++) {
unionSet[k++] = set1[i];
}
for (i = 0; i < n2; i++) {
for (j = 0; j < n1; j++) {
if (set2[i] == set1[j]) {
break;
}
}
if (j == n1) {
unionSet[k++] = set2[i];
}
}
printf("Union of the two sets is: ");
for (i = 0; i < k; i++) {
printf("%d ", unionSet[i]);
}
}
int main() {
int set1[] = {1, 2, 3, 4, 5};
int set2[] = {4, 5, 6, 7, 8};
int n1 = sizeof(set1) / sizeof(set1[0]);
int n2 = sizeof(set2) / sizeof(set2[0]);
findUnion(set1, set2, n1, n2);
return 0;
}
在这个示例中,我们定义了两个集合set1和set2,然后调用findUnion函数来计算它们的并集并输出结果。