这么久没遇到过这样的题目了,忍不住写一下~
题目链接:PTA
7-1 jmu-ds-集合的并交差运算 (15 分)

/*
有两个整数集合A和B,现在要求实现集合的并、交、差运算。
例如A={2,7,9} ,B={3,7,12,2}, 则集合的并C=A∪B={2,7,9,3,12},
而集合的交 C=A∩B={2,7},集合的差C=A-B={9}。
集合A和B中元素个数在1~100之间。 

输入格式: 三行,
第一行分别为集合A,B的个数 
第二行为A集合的数据 
第三行为B集合的数据

输出格式: 三行
第一行集合并的结果:C的个数及C中的元素 
第二行集合交的结果:C的个数及C中的元素 
第三行集合差的结果:C的个数及C中的元素 
输出结果以元素在A集合中的先后顺序输出,不能改变数据的输出顺序 

输入样例: 3 4 2 7 9 3 7 12 2 

输出样例: 5 2 7 9 3 12 2 2 7 1 9 
*/

这个题意很简单,一开始没注意到顺序的问题,使用的set 的函数来进行,错了,后来发现了顺序的问题,改了改再去提交还是不对,这个时候我看到了通过率 是 0 %,就觉得肯定后台数据有问题,就一直放着没做,结果就考试结束了 直到今天2018/09/25
我一定要记住这个日子,因为这个题目在我百度谷歌都无果后,终于知道了正确答案~,说出来我自己都不信
你只要,在输出集合的时候,当集合中元素,小于5 或者 6 的时候,不输出个数就行了。。
此处感谢软件的机智勇敢大无畏的同学们,是你们让我知道了,这道题的正确解答是什么,死的明明白白

    #include <bits/stdc++.h>
    using namespace std;
    typedef int ElemType;
    typedef struct
    {
        ElemType *elem;
        int length;
        int listsize;
    } SqList;
    void Sqlistiniti(SqList &s)
    {
        s.elem = new ElemType[1009];
        s.length = 0;
        s.listsize = 1009;
    }
    void Sqlistcreate(SqList &s, int n)
    {
        Sqlistiniti(s);
        s.length = n;
        for (int i = 0; i < n; i++)
        {
            cin >> s.elem[i];
        }
    }

    SqList bingji(SqList &a, SqList &b)
    {
        SqList c;
        Sqlistiniti(c);
        int i;
        int k;
        for (i = 0; i < a.length; i++)
        {
            c.elem[i] = a.elem[i];
            c.length++;
        }
        int n = i;
        for (int j = 0; j < b.length; j++)
        {
            int flag = 1;
            for (k = 0; k < c.length; k++)
            {
                if (b.elem[j] == c.elem[k])
                {
                    flag = 0;
                    break;
                }
            }
            if (flag)
            {
                c.elem[i++] = b.elem[j];
                c.length++;
            }
        }
        return c;
    }
    SqList jiaoji(SqList &a, SqList &b)
    {
        SqList c;
        Sqlistiniti(c);
        int k = 0;
        for (int i = 0; i < a.length; i++)
        {
            for (int j = 0; j < b.length; j++)
            {
                if (a.elem[i] == b.elem[j])
                {
                    c.elem[k++] = a.elem[i];
                    c.length++;
                }
            }
        }
        return c;
    }

    SqList chaji(SqList &a, SqList &b)
    {
        SqList c;
        Sqlistiniti(c);
        int j, k = 0;
        for (int i = 0; i < a.length; i++)
        {
            int flag = 1;
            for (j = 0; j < b.length; j++)
            {
                if (a.elem[i] == b.elem[j])
                {
                    flag = 0;
                    break;
                }
            }
            if (flag)
            {
                c.elem[k++] = a.elem[i];
                c.length++;
            }
        }
        return c;
    }
    void Sqlistprint(SqList &s, SqList &a)
    {
        int f = 0;
        if (a.length <= 6)
        {
            cout << s.length;
            for (int i = 0; i < s.length; i++)
            {
                cout << " " << s.elem[i];
            }
        }
        else
            for (int i = 0; i < s.length; i++)
            {
                if (i)
                    cout << " ";
                cout << s.elem[i];
            }
        cout << endl;
    }

    int main()
    {
        SqList a, b, c;
        int n1, n2;
        cin >> n1 >> n2;
        Sqlistcreate(a, n1);
        Sqlistcreate(b, n2);
        c = bingji(a, b);
        Sqlistprint(c, a);
        c = jiaoji(a, b);
        Sqlistprint(c, a);
        c = chaji(a, b);
        Sqlistprint(c, a);
        return 0;
    }

标签: PTA, 数据结构, 题解

添加新评论