0
点赞
收藏
分享

微信扫一扫

牛客:第k小数题解

悲催博士僧 2022-01-15 阅读 40

题目描述

给你一个长度为n的序列,求序列中第k小数的多少。

输入描述

多组输入,第一行读入一个整数T表示有T组数据。

每组数据占两行,第一行为两个整数n,k,表示数列长度和k。

第二行为n个用空格隔开的整数。

输出描述:

对于每组数据,输出它的第k小数是多少。

每组数据之间用空格隔开

示例


输入

2
5 2
1 4 2 3 4
3 3
3 2 1

输出

2
3

备注:

链接:登录—专业IT笔试面试备考平台_牛客网
来源:牛客网
 

t≤10,1≤n≤5×106,k≤n,数列里每个数都在int范围内

由于输入比较多,请使用快读读取。

例如:

inline int read(){
    int x = 0, f = 1;
    char ch = getchar();
    while(ch < '0' || ch > '9'){
        if (ch == '-')
            f = -1;
        ch = getchar();
    }
    while(ch >= '0' && ch <= '9'){
        x = (x<<1) + (x<<3) + (ch^48);
        ch = getchar();
    }
    return x * f;
}

代码如下

#include<iostream>
#include<algorithm>
using namespace std;

const int N = 1e6 + 10;
int arr[N];

inline int read() {
    int x = 0, f = 1;
    char ch = getchar();
    while (ch < '0' || ch > '9') {
        if (ch == '-')
            f = -1;
        ch = getchar();
    }
    while (ch >= '0' && ch <= '9') {
        x = (x << 1) + (x << 3) + (ch ^ 48);
        ch = getchar();
    }
    return x * f;
}
int main()
{
	int t;
	cin >> t;
	while (t--)
	{
		int n, k;
        n = read(), k = read();
        for (int i = 0; i < n; i++)
        {
            arr[i] = read();
        }
        sort(arr, arr + n);
        cout << arr[k - 1] << endl;
	}
	return 0;
}

​​​​​​t原题链接:https://ac.nowcoder.com/acm/problem/207028icon-default.png?t=LBL2https://ac.nowcoder.com/acm/problem/207028

举报

相关推荐

0 条评论