题意:
将两个整型数组按照升序合并,并且过滤掉重复数组元素。
输出合并之后的数组。
方法一:
快排
思路:将两个数组合并,再快排,最后过滤掉重复数组元素并输出。
#include <bits/stdc++.h>
using namespace std;
int a[1000005],n,m,num,x;
int main(){
while(cin >> n){//输入数据
num=0;
while(n--){
cin >> x;
a[num++]=x;
}
cin >> m;
while(m--){
cin >> x;
a[num++]=x;
}
sort(a,a+num);//排序
cout << a[0];
for(int i=1;i<num;i++){//过滤掉重复数组元素
if(a[i]!=a[i-1])
cout << a[i];
}
cout << endl;
}
return 0;
}
时间复杂度:
空间复杂度:![]()
方法二:
集合set
思路:利用有序set实现排序和去重效果。
将输入数据插入集合中,最后直接输出集合中的元素。
#include <bits/stdc++.h>
using namespace std;
int n,m,x;
set<int> st;
int main(){
while(cin >> n){//输入数据
st.clear();//集合清空初始化
while(n--){
cin >> x;
st.insert(x);//将数据插入集合中
}
cin >> m;
while(m--){
cin >> x;
st.insert(x);//将数据插入集合中
}
for(auto x:st){//输出
cout << x;
}
cout << endl;
}
return 0;
}
时间复杂度:
空间复杂度:![]()



京公网安备 11010502036488号