C++:怎么用顺序表算集合的差集和交集
1个回答
2018-10-22 · 知道合伙人互联网行家
关注
展开全部
#include<iostream>
#include<vector>
#include<cstdlib>
#include<algorithm>
#include<cstring>
using namespace std;vector<int> a;
vector<int> b;
void AND()
{
bool f[2000];
vector<int>::iterator iter1;
vector<int>::iterator iter2;
memset(f,false,sizeof(f));
int flag = 0;
for(iter1=a.begin();iter1!=a.end();iter1++)
{
for(iter2=b.begin();iter2!=b.end();iter2++)
{
if((*iter1)==(*iter2))
{
if(f[(*iter1)]==false)
{
flag = 1;
cout << *iter1 << " ";
f[(*iter1)] = true;
}
}
}
}
if(flag==0)
cout << "空集" << endl;
else cout << endl;
} void OR()
{
bool f[2000];
vector<int>::iterator iter1;
vector<int>::iterator iter2;
memset(f,false,sizeof(f));
for(iter1=a.begin();iter1!=a.end();iter1++)
{
f[*iter1] = true;
cout << *iter1 << " ";
}
for(iter2=b.begin();iter2!=b.end();iter2++)
{
if(f[*iter2]==false)
{
cout << *iter2 << " ";
}
}
cout << endl;
}
void CHA()
{
vector<int>::iterator iter1;
vector<int>::iterator iter2;
int flag = 0;
for(iter1=a.begin();iter1!=a.end();iter1++)
{
flag = 0;
for(iter2=b.begin();iter2!=b.end();iter2++)
{
if((*iter1)==(*iter2))
{
flag = 1;
}
}
if(flag==0)
{
cout << *iter1 << " ";
}
}
if(flag==1)
cout << "空集" << endl;
else cout << endl;
}int main()
{
int i,n,temp;
printf("请输入集合a个数为:");
scanf("%d",&n);
printf("请输入集合a的元素,以空格分开:");
for(i=1;i<=n;i++)
{
cin >> temp ;
a.push_back(temp);
}
printf("请输入集合b个数为:");
scanf("%d",&n);
printf("请输入集合b的元素,以空格分开:");
for(i=1;i<=n;i++)
{
cin >> temp ;
b.push_back(temp);
}
cout << "两集合的交集为:";
AND();
cout << "两集合的并集为:";
OR();
cout << "两集合的差集为:";
CHA();
system("pause");
return 0;
}
#include<vector>
#include<cstdlib>
#include<algorithm>
#include<cstring>
using namespace std;vector<int> a;
vector<int> b;
void AND()
{
bool f[2000];
vector<int>::iterator iter1;
vector<int>::iterator iter2;
memset(f,false,sizeof(f));
int flag = 0;
for(iter1=a.begin();iter1!=a.end();iter1++)
{
for(iter2=b.begin();iter2!=b.end();iter2++)
{
if((*iter1)==(*iter2))
{
if(f[(*iter1)]==false)
{
flag = 1;
cout << *iter1 << " ";
f[(*iter1)] = true;
}
}
}
}
if(flag==0)
cout << "空集" << endl;
else cout << endl;
} void OR()
{
bool f[2000];
vector<int>::iterator iter1;
vector<int>::iterator iter2;
memset(f,false,sizeof(f));
for(iter1=a.begin();iter1!=a.end();iter1++)
{
f[*iter1] = true;
cout << *iter1 << " ";
}
for(iter2=b.begin();iter2!=b.end();iter2++)
{
if(f[*iter2]==false)
{
cout << *iter2 << " ";
}
}
cout << endl;
}
void CHA()
{
vector<int>::iterator iter1;
vector<int>::iterator iter2;
int flag = 0;
for(iter1=a.begin();iter1!=a.end();iter1++)
{
flag = 0;
for(iter2=b.begin();iter2!=b.end();iter2++)
{
if((*iter1)==(*iter2))
{
flag = 1;
}
}
if(flag==0)
{
cout << *iter1 << " ";
}
}
if(flag==1)
cout << "空集" << endl;
else cout << endl;
}int main()
{
int i,n,temp;
printf("请输入集合a个数为:");
scanf("%d",&n);
printf("请输入集合a的元素,以空格分开:");
for(i=1;i<=n;i++)
{
cin >> temp ;
a.push_back(temp);
}
printf("请输入集合b个数为:");
scanf("%d",&n);
printf("请输入集合b的元素,以空格分开:");
for(i=1;i<=n;i++)
{
cin >> temp ;
b.push_back(temp);
}
cout << "两集合的交集为:";
AND();
cout << "两集合的并集为:";
OR();
cout << "两集合的差集为:";
CHA();
system("pause");
return 0;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询