题目描述
给定一个整数数组a[0,...,n-1],求数组中第k小数
输入描述首先输入数组长度n和k,其中1<=n<=5000, 1<=k<=n
然后输出n个整形元素,每个数的范围[1, 5000]
输出描述该数组中第k小数
样例输入4 2 1 2 3 4
样例输出
2
/*students please write your program here*/ #include <iostream> using namespace std; int main() { int a[5000]; int n, k; cin >> n; cin >> k; for (int i = 0; i < n; i++) cin >> a[i]; int temp; for(int j = 0;j < k;j++) for (int i = n - 1; i > j; i--) { if (a[i-1] > a[i]) { temp = a[i]; a[i] = a[i - 1]; a[i - 1] = temp; } } cout <<a[k-1] << endl; }