本文发布于 1578 天前,最后更新于 1457 天前,其中的信息可能已经有所发展或是发生改变。
斌斌开飞机了!!!布置这么多单纯耗费时间的题,要人命了
#include <bits/stdc++.h>
using namespace std;
#define LL long long
#define ElemType int // 算是泛化吧
//!!!UTF-8!!!
/*
使用顺序表存储集合,
并实现集合的交集、并集、差集(即A-B)、对称差(即A-B并B-A)、
判断一个元素是否属于一个集合、判断两个集合是否相等。
计算机科学中,集合是一组可变数量的数据项(也可能是0个)的组合,
这些数据项可能共享某些特征,需要以某种操作方式一起进行操作。
一般来讲,这些数据项的类型是相同的,或基类相同(若使用的语言支持继承)。
列表(或数组)通常不被认为是集合,因为其大小固定,但事实上它常常在实现中作为某些形式的集合使用。
集合的种类包括列表,集,多重集,树和图。枚举类型可以是列表或集。
集合论中,设A,B是两个集合,由所有属于集合A且属于集合B的元素所组成的集合,
叫做集合A与集合B的交集(intersection),记作A∩B。
一般地,设A,B是两个集合,由所有属于A且不属于B的元素组成的集合,
叫做集合A减集合B(或集合A与集合B之差),类似地,对于集合A.B,
我们把集合{x/x∈A,且x¢B}叫做A与B的差集,记作A-B记作A-B(或A\B),即A-B={x|x∈A,且x
*/
class SqList {
public:
int len=0,size=0;
ElemType *pData;
void Create(int Size)
{
size = Size;
pData=new ElemType[size];
len = 0;
}
void Delete(int pos)
{
len--;
for(int i=pos; i<len-1; i++)
pData[i]=pData[i+1];
}
void Destroy()
{
size=len=0;
delete pData; // delete自身会自动检查对象是否为空.如果为空,就不做操作...
pData = nullptr;
}
void Insert(ElemType Data,int pos)
{
if(len==size)
{
size +=100;
pData=(ElemType*)realloc(pData,sizeof(pData)+100*sizeof(ElemType));
}
for(int i=len-1; i>=pos; i--)
pData[i+1]=pData[i];
pData[pos]=Data;
len++;
}
void Cout()
{
for(int i=0; i<len; i++)
cout<<pData[i]<<" ";
}
public:
ElemType *Jiaoji(ElemType a[],int alen,ElemType b[],int blen)
{
if(alen>blen);
else swap(a,b),swap(alen,blen);
bool pd[1008611];
ElemType *ans;
ans = new ElemType[alen];
int num=1;
for(int i=0; i<alen; i++)
for(int j=i; j<blen; j++)
if(a[i]==b[j]&&!pd[a[i]])
{
ans[num++]= a[i];
pd[a[i]]=1;
}
ans[0] = num; // 存储长度信息
return ans;
}
ElemType *Bingji(ElemType a[],int alen,ElemType b[],int blen)
{
int len = alen+blen,num=1;
ElemType *ans = new ElemType[len];
bool pd[1008611];
memset(pd,0,sizeof(pd));
for(int i=0; i<alen; i++)
if(!pd[a[i]%1008611])
{
ans[num++]=a[i];
pd[a[i]%1008611]=true;
}
for(int i=0; i<blen; i++)
if(!pd[b[i]%1008611])
{
ans[num++]=b[i];
pd[b[i]%1008611]=true;
}
ans[0] = num;
return ans;
}
ElemType *Chaji(ElemType a[],int alen,ElemType b[],int blen)
{
int len = max(alen,blen),num=1;
ElemType *ans = new ElemType[len];
bool pd[1008611];
memset(pd,0,sizeof(pd));
for(int i=0; i<alen; i++)
if(!pd[a[i]%1008611])
pd[a[i]%1008611]=true;
for(int i=0; i<blen; i++)
if(!pd[b[i]%1008611])
pd[b[i]%1008611]=false;
for(int i=0; i<alen; i++)
if(pd[a[i]%1008611])
ans[num++] = a[i];
ans[0] = num;
return ans;
}
ElemType *Duichencha(ElemType a[],int alen,ElemType b[],int blen)
{
int len = alen+blen,num=1;
ElemType *ans = new ElemType[len];
bool pd[1008611];
memset(pd,0,sizeof(pd));
for(int i=0; i<alen; i++)
if(!pd[a[i]%1008611])
pd[a[i]%1008611]=true;
for(int i=0; i<blen; i++)
if(!pd[b[i]%1008611])
pd[b[i]%1008611]=false;
for(int i=0; i<alen; i++)
if(pd[a[i]%1008611])
ans[num++] = a[i];
memset(pd,0,sizeof(pd));
for(int i=0; i<alen; i++)
if(!pd[b[i]%1008611])
pd[b[i]%1008611]=true;
for(int i=0; i<blen; i++)
if(!pd[a[i]%1008611])
pd[a[i]%1008611]=false;
for(int i=0; i<alen; i++)
if(pd[b[i]%1008611])
ans[num++] = b[i];
ans[0] = num;
return ans;
}
bool PdShuyu(ElemType a[],int alen,ElemType b) // 判断元素 b 是否属于集合 a
{
for(int i=0; i<alen; i++)
if(a[i]==b)
return true;
return false;
}
bool PdJiheXiangdeng(ElemType a[],int alen,ElemType b[],int blen)
{
if(alen!=blen) return false;
for(int i=0; i<alen; i++)
if(a[i]!=b[i]) return false;
return true;
}
};
int main()
{
//不说元素类型,泛式太麻烦,就用整形了
int aa[9] = {1,2,3,4,5,6,7,8,9}; // 用于测试数据填充
int bb[9] = {10,11,2,4,6,8,37,4,5};
SqList a,b,temp;
a.Create(5);
b.Create(5);
for(int i=0; i<9; i++)
{
a.Insert(aa[i],i);
b.Insert(bb[i],i);
}
cout <<"The original data of A "; a.Cout(); cout << endl;
cout <<"The original data of B "; b.Cout(); cout << endl;
cout <<"Sorted data of A "; a.Cout(); cout << endl;
cout <<"Sorted data of B "; b.Cout(); cout << endl;
cout << "Intersection: ";
for(int i=1; i<temp.Jiaoji(a.pData,a.len,b.pData,b.len)[0]; i++)
cout << temp.Jiaoji(a.pData,a.len,b.pData,b.len)[i]<<" ";
cout << endl;
cout << "Union: ";
for(int i=1; i<temp.Bingji(a.pData,a.len,b.pData,b.len)[0]; i++)
cout << temp.Bingji(a.pData,a.len,b.pData,b.len)[i]<<" ";
cout << endl;
cout << "Subtraction: ";
for(int i=1; i<temp.Chaji(a.pData,a.len,b.pData,b.len)[0]; i++)
cout << temp.Chaji(a.pData,a.len,b.pData,b.len)[i]<<" ";
cout << endl;
cout << "Symmetrical difference: ";
for(int i=1; i<temp.Duichencha(a.pData,a.len,b.pData,b.len)[0]; i++)
cout << temp.Duichencha(a.pData,a.len,b.pData,b.len)[i]<<" ";
cout << endl;
cout << "Determine whether an element belongs to a collection";
cout << endl << "Element is 9" << " " <<"Set as A "<< endl;
if(temp.PdShuyu(a.pData,a.len,9))
cout << "Belong";
else cout << "Not Belong";
cout << endl;
cout << "Determine whether two sets are equal"<<endl;
if(temp.PdJiheXiangdeng(a.pData,a.len,b.pData,b.len))
cout << "Equal";
else cout << "Not Equal";
cout << endl;
}