There are two groups of some numbers(0~50). each group should be input in a set(remove the duplicate numbers).
And then output the two set, intersection and union of the two set.
Input format:
first line: a couple of numbers and end of -1.
second line: a couple of numbers and end of -1.
Output format:
first line:Output the first set.
second line: Output the second set.
third line: Output the intersection of the two set.
fourth line:Output the union of the two set.
All the numbers in the set should be output by ascending oder.
Each line behind the numbers, there is a space ’ ‘.
For example:
[Input]
1 2 3 6 5 4 1 2 3 -1
3 2 3 2 1 0 -1
[Output]
1 2 3 4 5 6
0 1 2 3
1 2 3
0 1 2 3 4 5 6
[Input]
0 -1
1 -1
[Output]
0
1
0 1
#include <stdio.h>
#include <stdlib.h>
int comp(const void*a, const void*b) {
return *(int *)a - *(int *)b;
}
int input1[100] = {0};
int input2[100] = {0};
void remove_duplicate(int input[], int length) {
qsort(input, length, sizeof(input[0]), comp);
for (int j = 0; j < length - 1; j++) {
if (input[j] == input1[j + 1]) {
input[j] = -1;
}
}
}
void print(int input[], int length) {
for (int i = 0; i < length; i++) {
if (input[i] != -1) {
printf("%d ", input[i]);
}
}
printf("\n");
}
void print_the_intersection(int input[], int length1, int length2) {
int flag = 1;
for (int i = 0; i < length1; i++) {
for (int j = 0; j < length1; j++) {
if (input[i] == -1) {
continue;
}
if (input[i] == input1[j]) {
flag = 0;
break;
}
}
if (flag == 0 && input[i] != -1) {
printf("%d ", input[i]);
}
flag = 1;
}
printf("\n");
}
int main() {
int a;
scanf("%d", &a);
int length1 = 0;
while (a != -1) {
input1[length1++] = a;
scanf(" %d", &a);
}
remove_duplicate(input1, length1);
int length2 = 0;
scanf(" %d", &a);
while (a != -1) {
input2[length2++] = a;
scanf(" %d", &a);
}
remove_duplicate(input2, length2);
print(input1, length1);
print(input2, length2);
if (length1 > length2) {
print_the_intersection(input2, length2, length1);
} else {
print_the_intersection(input1, length1, length2);
}
int lastanswer[200] = {0};
for (int i = 0; i < length1; i++) {
lastanswer[i] = input1[i];
}
for (int i = length1; i < length1 + length2; i++) {
lastanswer[i] = input2[i - length1];
}
qsort(lastanswer, length1 + length2, sizeof(lastanswer[0]), comp);
for (int i = 0; i < length1 + length2 - 1; i++) {
if (lastanswer[i] == lastanswer[i + 1]) {
lastanswer[i] = -1;
}
}
for (int i = 0; i < length1 + length2; i++) {
if (lastanswer[i] != -1) {
printf("%d ", lastanswer[i]);
}
}
printf("\n");
return 0;
}