华为机试 HJ80 题解 | #整型数组合并#
整型数组合并
https://www.nowcoder.com/practice/c4f11ea2c886429faf91decfaf6a310b
#include <iostream> #include <vector> #include <algorithm> #include <set> using namespace std; int main() { int n1 = 0, n2 = 0; vector<int> nVec1; vector<int> nVec2; cin >> n1; int temp = 0; for (int i = 0; i < n1; i++) { cin >> temp; nVec1.push_back(temp); } cin >> n2; for (int i = 0; i < n2; i++) { cin >> temp; nVec2.push_back(temp); } vector<int> nVec3 = nVec2; // 去掉nVec1和nVec2中的重复数组元素 for (auto iter1 = nVec1.begin(); iter1 != nVec1.end(); ++iter1) { int x = *iter1; auto jter = std::find_if(nVec2.begin(), nVec2.end(), [x](int item) { return x == item; }); if (jter == nVec2.end()) { // 对于nVec2中没有而nVec1有的元素,添加到nVec3中 nVec3.push_back(x); } } // 对合并后的nVec3进行升序排序 std::sort(nVec3.begin(), nVec3.end()); // 过滤掉nVec3中的重复元素 set<int> nSet; for (auto item : nVec3) { nSet.insert(item); } for (auto item : nSet) { cout << item; } cout << endl; }