问题描述
给出两个整数集合A、B,求出他们的交集、并集以及B在A中的余集。
输入格式
第一行为一个整数n,表示集合A中的元素个数。
第二行有n个互不相同的用空格隔开的整数,表示集合A中的元素。
第三行为一个整数m,表示集合B中的元素个数。
第四行有m个互不相同的用空格隔开的整数,表示集合B中的元素。
集合中的所有元素均为int范围内的整数,n、m<=1000。
第二行有n个互不相同的用空格隔开的整数,表示集合A中的元素。
第三行为一个整数m,表示集合B中的元素个数。
第四行有m个互不相同的用空格隔开的整数,表示集合B中的元素。
集合中的所有元素均为int范围内的整数,n、m<=1000。
输出格式
第一行按从小到大的顺序输出A、B交集中的所有元素。
第二行按从小到大的顺序输出A、B并集中的所有元素。
第三行按从小到大的顺序输出B在A中的余集中的所有元素。
第二行按从小到大的顺序输出A、B并集中的所有元素。
第三行按从小到大的顺序输出B在A中的余集中的所有元素。
样例输入
5
1 2 3 4 5
5
2 4 6 8 10
1 2 3 4 5
5
2 4 6 8 10
样例输出
2 4
1 2 3 4 5 6 8 10
1 3 5
1 2 3 4 5 6 8 10
1 3 5
样例输入
4
1 2 3 4
3
5 6 7
1 2 3 4
3
5 6 7
样例输出
1 2 3 4 5 6 7
1 2 3 4
这次这个没什么技术难度单纯拿出发下而已!
#include <iostream>
#include <string>
#include <queue>
using namespace std;
void sort(int *arr, int low,int high){
if(low>=high){
return;
}
int left = low;
int right = high;
int key = arr[low];
while(left<right){
while((left<right)&&(arr[right]>=key))right--;
arr[left] = arr[right];
while((left<right)&&(arr[left]<=key))left++;
arr[right] = arr[left];
}
arr[left] = key;
sort(arr,low,left-1);
sort(arr,left+1,high);
}
int main()
{
int* A;
int* B;
int n,m;
queue<int> hand_set,and_set,last_set;
cin>>n;
A = new int[n];
for(int i = 0; i<n; i++){
cin>>A[i];
}
cin>>m;
B = new int[m];
for(int i = 0; i<m; i++){
cin>>B[i];
}
sort(A,0,n-1);
sort(B,0,m-1);
int i = 0,j = 0;
while((i!=n)||(j!=m)){
if(A[i]==B[j]){
hand_set.push(A[i]);
and_set.push(A[i]);
i++;
j++;
}
else if((i!=n)&&(j!=m)&&(A[i]<B[j])){
and_set.push(A[i]);
last_set.push(A[i]);
i++;
}else if(j!=m){
and_set.push(B[j]);
j++;
}else if(i!=n){
and_set.push(A[i]);
last_set.push(A[i]);
i++;
}
}
if(!hand_set.empty()){
while(!hand_set.empty()){
cout<<hand_set.front()<<' ';
hand_set.pop();
}
cout<<endl;
}
if(!and_set.empty()){
while(!and_set.empty()){
cout<<and_set.front()<<' ';
and_set.pop();
}
cout<<endl;
}
if(!last_set.empty()){
while(!last_set.empty()){
cout<<last_set.front()<<' ';
last_set.pop();
}
cout<<endl;
}
return 0;
}