算法训练 集合运算
2016-02-01 17:29
316 查看
问题描述
给出两个整数集合A、B,求出他们的交集、并集以及B在A中的余集。
输入格式
第一行为一个整数n,表示集合A中的元素个数。
第二行有n个互不相同的用空格隔开的整数,表示集合A中的元素。
第三行为一个整数m,表示集合B中的元素个数。
第四行有m个互不相同的用空格隔开的整数,表示集合B中的元素。
集合中的所有元素均为int范围内的整数,n、m<=1000。
输出格式
第一行按从小到大的顺序输出A、B交集中的所有元素。
第二行按从小到大的顺序输出A、B并集中的所有元素。
第三行按从小到大的顺序输出B在A中的余集中的所有元素。
样例输入
5
1 2 3 4 5
5
2 4 6 8 10
样例输出
2 4
1 2 3 4 5 6 8 10
1 3 5
样例输入
4
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
;
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;
}
给出两个整数集合A、B,求出他们的交集、并集以及B在A中的余集。
输入格式
第一行为一个整数n,表示集合A中的元素个数。
第二行有n个互不相同的用空格隔开的整数,表示集合A中的元素。
第三行为一个整数m,表示集合B中的元素个数。
第四行有m个互不相同的用空格隔开的整数,表示集合B中的元素。
集合中的所有元素均为int范围内的整数,n、m<=1000。
输出格式
第一行按从小到大的顺序输出A、B交集中的所有元素。
第二行按从小到大的顺序输出A、B并集中的所有元素。
第三行按从小到大的顺序输出B在A中的余集中的所有元素。
样例输入
5
1 2 3 4 5
5
2 4 6 8 10
样例输出
2 4
1 2 3 4 5 6 8 10
1 3 5
样例输入
4
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
;
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;
}
相关文章推荐
- 渲染农场
- bzoj2762 不等式组 树状数组
- Android 编码规范及代码风格
- 寄存器操作
- Java基础知识梳理
- Android 自定义Dialog以及参数的传递
- Oracle Update分批提交
- ElecAssist
- java 深入理解异常处理机制
- 接口隔离原则
- icare3.0中医生诊间开出检验检查保存提交失败--------运维日志17
- 《java设计模式》之工厂模式
- Myeclipse通过表生成实体类和注解
- 跨域请求之jQuery的ajax jsonp的使用解惑
- 浅谈XListView的使用
- 使用 pjax 实现无刷新切换页面
- Linux防火墙之具有命令行的FirewallD的基本操作
- 到期自动删除
- User Agent
- SpringMVC拦截器(资源和权限管理)