集合A,B用LA,LB表示,第一行输入A和B元素个数,第二行第三行分别输入元素,输出集合A,B和并集C ,C语言
时间: 2024-06-09 12:05:03 浏览: 71
```c
#include <stdio.h>
#define MAX_SIZE 100
int main() {
int la[MAX_SIZE], lb[MAX_SIZE], c[MAX_SIZE * 2];
int n, m, i, j, k;
printf("Please enter the number of elements in set A: ");
scanf("%d", &n);
printf("Please enter the elements of set A: ");
for (i = 0; i < n; i++) {
scanf("%d", &la[i]);
}
printf("Please enter the number of elements in set B: ");
scanf("%d", &m);
printf("Please enter the elements of set B: ");
for (i = 0; i < m; i++) {
scanf("%d", &lb[i]);
}
i = j = k = 0;
while (i < n && j < m) {
if (la[i] < lb[j]) {
c[k++] = la[i++];
} else if (lb[j] < la[i]) {
c[k++] = lb[j++];
} else {
c[k++] = la[i++];
j++;
}
}
while (i < n) {
c[k++] = la[i++];
}
while (j < m) {
c[k++] = lb[j++];
}
printf("Set A: ");
for (i = 0; i < n; i++) {
printf("%d ", la[i]);
}
printf("\nSet B: ");
for (i = 0; i < m; i++) {
printf("%d ", lb[i]);
}
printf("\nUnion set C: ");
for (i = 0; i < k; i++) {
printf("%d ", c[i]);
}
return 0;
}
```
阅读全文