PTA:7-1 线性表的合并
线性表的合并
- 题目
- 输入样例
- 输出样例
- 代码
- 解析
题目
输入样例
4
7 5 3 11
3
2 6 3
输出样例
7 5 3 11 2 6
代码
#include<iostream>
#include<vector>
using namespace std;bool checkrep(const vector<int>& arr, int x)
{for (int element : arr) {if (element == x)return false;}return true;
}int main()
{int n1, n2;cin >> n1;vector<int> a(n1);for (int i = 0; i < n1; i++) {cin >> a[i];}cin >> n2;vector<int> b(n2);for (int j = 0; j < n2; j++) {cin >> b[j];}vector<int> result;for (int element : a) {result.push_back(element);}for (int element : b) {if (checkrep(a, element)) {result.push_back(element);}}for (int m = 0; m < result.size(); m++) {cout << result[m] << " ";}return 0;
}
解析
①函数checkrep
:检查重复项
②输入两组数据
③往集合C中push元素,注意检查重复元素
④随后输出结果,注意题目中给的示例,最后一项后面是有空格的,这里不需要单独处理;*有些题要求最后一项后无空格,需要再加一个条件判断