J
#include<stdio.h>
int read() {
char ch = getchar(); int x = 0, f = 1;//f 用于标记正负
while(ch < '0' || ch > '9')
{
if(ch == '-') f = -1;
ch = getchar();
}
while('0' <= ch && ch <= '9')//跳过非数字字符
{
x = x * 10 + ch - '0';//转换为整数
ch = getchar();
} return x * f;
}
int main(){
int t;
t=read();
while(t--)
{
int n,k,a[5000005]={0},i=0,p,sum=0;//a:出现次数,p临时存储读取的数
n=read();
k=read();
while(n--)
{
p=read();
a[p]++;
}
while(sum<k){
n++;
sum+=a[n];
}
printf("%d\n",n);
}
return 0;
◮:
◮:用一个数组 a 来统计每个数出现的次数。然后从小到大遍历这个统计数组,累加出现次数,当累加的次数达到或超过 k 时,当前的数就是第 k 小的数
浙公网安备 33010602011771号