第二章作业题1-顺序表-计算机17级 7-1 jmu-ds-集合的并交差运算 (15 分)

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

 

这题的输出,我过了也不懂是为啥。。。

#include <iostream>
#include <malloc.h>
using namespace std;

#define LIST_INIT_SIZE  100
#define LISTINCREMENT   10

#define TRUE        1
#define FALSE       0
#define OK          1
#define ERROR       0
#define INFEASIBLE -1
#define OVERFLOW   -2

typedef int ElemType;
typedef struct{
    ElemType *elem;
    int length;
    int listsize;
}SqList;

void SqListInit(SqList &S);
void SqListCreate_S(SqList &S,ElemType n);
SqList jiaoji(SqList &S1,SqList &S2);
SqList bingji(SqList &S1,SqList &S2);
SqList chaji(SqList &S1,SqList &S2);
//void SqListPrint(SqList &S);
void SqListPrint(SqList &s,SqList &a);




int main()
{
    SqList a,b,c;
    int m,n;
    cin>>m>>n;
    SqListCreate_S(a,m);
    SqListCreate_S(b,n);
    c = bingji(a,b);
    SqListPrint(c,a);
    c = jiaoji(a,b);
    SqListPrint(c,a);
    c = chaji(a,b);
    SqListPrint(c,a);
    return 0;
}

void SqListInit(SqList &S)
{
    S.elem = (ElemType*)malloc(LISTINCREMENT*sizeof(ElemType));
    if(!S.elem)
        exit(OVERFLOW);
    S.length = 0;
    S.listsize = LIST_INIT_SIZE;
}

void SqListCreate_S(SqList &S,ElemType n)
{
    SqListInit(S);
    S.length = n;
    for(int i = 0;i < n; i++)
    {
        cin>>S.elem[i];
    }
}

SqList bingji(SqList &S1,SqList &S2)
{
    SqList S;
    SqListInit(S);
    int i,j;
    for(i = 0;i<S1.length;i++)
    {
        S.elem[i] = S1.elem[i];
        S.length++;
    }
    for(j=0;j<S2.length;j++)
    {
        int flag = 1;
        for(int k = 0;k<S.length;k++)
        {
            if(S2.elem[j] == S.elem[k])
            {
                flag = 0;
                break;
            }
        }
        if(flag)
        {
            S.elem[i++] = S2.elem[j];
            S.length++;
        }
    }
    return S;

}
void SqListPrint(SqList &s,SqList &a)
{
    //int f=0;
    if(a.length<=9)
    {
        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;
}


/*

void SqListPrint(SqList &c)
{
    cout<<c.length;
    for(int i = 0;i<c.length;i++)
    {
        cout<<" "<<c.elem[i];
    }
    cout<<endl;
}

*/

SqList jiaoji(SqList &S1,SqList &S2)
{
    SqList S;
    SqListInit(S);
    int i,j;
    int k = 0;
    for(i = 0;i<S1.length;i++)
    {
        for(j = 0;j<S2.length;j++)
        {
            if(S1.elem[i] == S2.elem[j])
            {
                S.elem[k++] = S1.elem[i];
                S.length++;
            }
        }
    }
    return S;
}

SqList chaji(SqList &S1,SqList &S2)
{
    SqList S;
    SqListInit(S);
    int i,j;
    int k = 0;
    for(i = 0;i<S1.length;i++)
    {
        int flag = 1;
        for(j = 0;j<S2.length;j++)
        {
            if(S1.elem[i] == S2.elem[j])
            {
                flag = 0;
                break;
            }
        }
        if(flag)
        {
            S.elem[k++] = S1.elem[i];
            S.length++;
        }
    }
    return S;
}

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值