总结
CSDN竞赛越来越频繁了,这次竞赛比较简单,就当练习题了。
题目列表
1.求并集
题目描述
由小到大输出两个单向有序链表的并集 如链表 A {1 -> 2 -> 5 -> 7} 链表 B {3 -> 5 -> 7 -> 8} 输出: {1 -> 2 ->3 - > 5 -> 7 ->8} 。
输入描述:
第一行输入整数n,m,(1<=n,m<=1000)分别表示两个链表的长度。
第二行给出A链表所包含元素。(1<=a<=1000)
第三行给出B链表所包含元素。(1<=b<=1000)
输出描述:
按题目描述输出。
输入样例:
4 4
1 2 5 7
3 5 7 8
输出样例:
1 -> 2 ->3 -> 5 -> 7 ->8
分析
这道题作为面试题,考察链表比较合适,作为笔试题不太合适。一般的做法是使用类似于归并排序的合并算法,使用双指针来合并。但是完全没必要这么做,直接用一个数组读入,然后排下序就可以了。
当然,这题的用例告诉我们需要去重,所以可以读入一个set中,然后再排序就可以了。而实际上set已经默认排好序了,直接输出就可以了。
代码
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <algorithm>
#include <set>
using namespace std;
set<int> s;
int main() {
int n;
int m;
cin>>n>>m;
vector<int> a;
int x;
for(int i = 0; i < n + m; i++) {
cin>>x;
s.insert(x);
}
for(auto t : s) {
a.push_back(t);
}
int q = a.size();
for(int i = 0; i < q - 1; i++) {
cout<<a[i]<<" -> ";
}
cout<<a[q - 1]<<endl;
return 0;
}
2.小T找糖豆
题目描述
已知连续序列A,包含1e18个元素,分别是[1,1e8]。 现在去除序列中的n个元素. 得到新的连续序列[X,1e8],该序列中 最小元素是?
分析
本题的题意是去掉若干元素后,求剩下连续序列的最小元素,用例给我们的感觉是必须是最后一个连续序列,所以相当于求出去除序列的最大值,加上1就是连续序列的最小元素了。
代码
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <algorithm>
using namespace std;
int solution(int n, std::vector<int>& vec) {
int res = 0