1 条题解

  • 0
    @ 2023-6-11 12:22:22

    C :

    #include <stdio.h>
    #define LEN 2500
    
    int n,m;
    long long arr[LEN][2] = {0};/*0->存储数据,1->存储加点*/
    
    void sort_q(int x,int y,long long tmp[LEN][2])
    {
        int i = x , j = y;
        long long k[2];
        k[0] = tmp[x][0];
        k[1] = tmp[x][1];
        if (i < j)
        {
            while (i < j)
            {
                while (i < j && k[0] < tmp[j][0])
                    j --;
                if (i < j)
                {
                    tmp[i][0] = tmp[j][0];
                    tmp[i][1] = tmp[j][1];
                    i ++;
                }
                while (i < j && k[0] > tmp[i][0])
                    i ++;
                if (i < j)
                {
                    tmp[j][0] = tmp[i][0];
                    tmp[j][1] = tmp[i][1];
                    j --;
                }
            }
            tmp[i][0] = k[0];
            tmp[i][1] = k[1];
            sort_q(x,i-1,tmp);
            sort_q(i+1,y,tmp);
        }
        return ;
    }
    
    void init(void)
    {
        int flag;
        int i,j;
        long long k;
    
        scanf("%d",&n);
        for (i = 1 ; i <= n ; i ++)
        {
            scanf("%lld",&k);
            arr[i][0] = k;
            arr[i][1] += 1;
        }
    
        scanf("%d",&m);
        for (i = n+1 ; i <= n+m ; i ++)
        {
            scanf("%lld",&k);
            flag = 1;
            for (j = 1 ; j <= n ; j ++)
            {
                if (arr[j][0] == k)
                {
                    arr[j][1] += 2;
                    flag = 0;
                    break;
                }
            }
            if (flag)
            {
                arr[i][0] = k;
                arr[i][1] += 2;
            }
        }
        sort_q(1,i,arr);    /*快排*/
        return ;
    }
    
    void print()
    {
        int i;
        for (i = 1 ; i <= LEN ; i ++)
        {
            if (arr[i][1] >= 3)
            {
                printf("%lld ",arr[i][0]);/*输出AB交集*/
            }
        }
        printf("\n");
    
        for (i = 1 ; i <= LEN ; i ++)
        {
            if (arr[i][1] != 0)
            {
                printf("%lld ",arr[i][0]);/*输出AB并集*/
            }
        }
        printf("\n");
    
        for (i = 1 ; i <= LEN ; i ++)
        {
            if (arr[i][1] == 1)
            {
                printf("%lld ",arr[i][0]);/*输出B在A中的余集*/
            }
        }
    
        return ;
    }
    
    int main(void)
    {
        init();
        print();
        return 0;
    }
    

    C++ :

    #include <stdio.h>
    #include <stdlib.h>
     
    int cmp(const void *a, const void *b)
    {
        return *(int *)a - *(int *)b;
    }
     
    void intersection_set(int *a, int size_a, int *b, int size_b)
    {
        int i = 0, j = 0;
        while (i < size_a && j < size_b)
        {
            if (a[i] == b[j])
            {
                printf("%d ", a[i]);
                ++i;
                ++j;
            }
            else if (a[i] < b[j])
                ++i;
            else
                ++j;
        }
        printf("\n");
    }
     
    void union_set(int *a, int size_a, int *b, int size_b)
    {
        int i = 0, j = 0;
        while (i < size_a && j < size_b)
        {
            if (a[i] == b[j])
            {
                printf("%d ", a[i]);
                ++i;
                ++j;
            }
            else if (a[i] < b[j])
            {
                printf("%d ", a[i]);
                ++i;
            }
            else
            {
                printf("%d ", b[j]);
                ++j;
            }
        }
        while (i < size_a)
        {
            printf("%d ", a[i]);
            ++i;
        }
        while (j < size_b)
        {
            printf("%d ", b[j]);
            ++j;
        }
        printf("\n");
    }
     
    void difference_set(int *a, int size_a, int *b, int size_b)
    {
        int i = 0, j = 0;
        while (i < size_a && j < size_b)
        {
            if (a[i] == b[j])
            {
                ++i;
                ++j;
            }
            else if (a[i] < b[j])
            {
                printf("%d ", a[i]);
                ++i;
            }
            else
                ++j;
        }
        while (i < size_a)
        {
            printf("%d ", a[i]);
            ++i;
        }
    }
     
    int main()
    {
        int n, m;
        int a[1002] = { 0 }, b[1002] = { 0 };
     
        scanf("%d", &n);
        for (int i = 0; i < n; ++i)
            scanf("%d", &a[i]);
        scanf("%d", &m);
        for (int i = 0; i < m; ++i)
            scanf("%d", &b[i]);
     
        qsort(a, n, sizeof(int), cmp);
        qsort(b, m, sizeof(int), cmp);
     
        intersection_set(a, n, b, m);
        union_set(a, n, b, m);
        difference_set(a, n, b, m);
     
        return 0;
    }
    

    Python :

    """
    @Author:Lixiang
    
    @Blog(个人博客地址): https://lixiang007.top/
    
    @WeChat:18845312866
    
    """
    import math
    import string
    import sys
    n=int(input())
    a=list(map(int,input().strip().split(" ")))
    m=int(input())
    b=list(map(int,input().strip().split(" ")))
    jiaoji=[]
    bingji=[]
    yuji=[]
    for i in a:
        if i in b:
            jiaoji.append(i)
    jiaoji=list(set(jiaoji))
    jiaoji.sort()
    for i in jiaoji:
        print(i,end=" ")
    print("")
    a1=a[:]
    b1=b[:]
    a1.extend(b1)
    a1=list(set(a1))
    a1.sort()
    for i in a1:
        print(i,end=" ")
    print("")
    for i in a:
        if i not in b:
            yuji.append(i)
    yuji.sort()
    for i in yuji:
        print(i,end=" ")
    
    
    
    • 1

    信息

    ID
    2722
    时间
    1000ms
    内存
    512MiB
    难度
    (无)
    标签
    递交数
    0
    已通过
    0
    上传者