网站建设公司山而新浪博客
【题目来源】
 https://www.acwing.com/problem/content/3536/
 
 【题目描述】
 查找一个长度为 n 的数组中第 k 小的数。
 注意,相同大小算一样大,如 2 1 3 4 5 2 中,第三小的数为 3。
 
 【输入格式】
 第一行包含整数 n。
 第二行包含 n 个整数,表示数组中元素。
 第三行包含整数 k。
 
 【输出格式】
 输出第 k 小的整数。
 数据保证有解。
 
 【数据范围】
 1≤k≤n≤1000
 数组中元素取值范围 [1,10000]
 
 【输入样例】
 6
 2 1 3 5 2 2
 3
 
 【输出样例】
 3
 
 【算法分析】
 ● 这是一道北京邮电大学考研机试题。
 
 ● 本代码利用 STL 中的 sort+unique 实现。
 sort 函数用法:https://blog.csdn.net/hnjzsyjyj/article/details/130524018
 unique 函数用法:https://blog.csdn.net/hnjzsyjyj/article/details/127197232
 切记,STL unique 函数并没有将重复的元素删去,而是依序排在了不重复元素的后面。
 
 ● 本题的 STL set 实现:https://blog.csdn.net/hnjzsyjyj/article/details/146330773
 
 ● 本题的 STL map 实现:https://blog.csdn.net/hnjzsyjyj/article/details/146330963
 
 【算法代码】
#include <bits/stdc++.h>
using namespace std;const int maxn=1e3+5;
int a[maxn];int main() {int n,k;cin>>n;for(int i=1; i<=n; i++) cin>>a[i];sort(a+1,a+1+n);unique(a+1,a+1+n);cin>>k;cout<<a[k];return 0;
}/*
in:
6
2 1 3 5 2 2
3out:
3
*/
 
 【参考文献】
 https://www.acwing.com/solution/content/272131/
 https://www.acwing.com/solution/content/96026/
 https://blog.csdn.net/hnjzsyjyj/article/details/146117096
  
  
