数组--清除相同,并集和交集

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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值