洛谷日刷 | Eternal_zttz

洛谷日刷

P1138 第K小整数

洛谷传送门

AC代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <cstdio>
#include <algorithm>
using namespace std;
int main(){
int n,k;
int num = 1;
scanf("%d %d",&n,&k);
int a[n],b[n];
for (int i =0 ;i<n;i++){
scanf("%d",&a[i]);
}
sort(a,a+n);
b[0] = a[0];
for (int i = 1;i<n;i++){
if(a[i]!=a[i-1])
b[num++] = a[i];
}
if(k-1>=num)
printf("NO RESULT");
else printf("%d",b[k-1]);
}

-------------The End-------------