在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函數來計算它們的并集并輸出結果。