2018-12-14 11:05

# 输入一个数组，求其中第K大的数字，K不一定是多少，怎么计算，用C语言

Problem Description
Alice are given an array A[1..N] with N numbers.

Now Alice want to build an array B by a parameter K as following rules:

Initially, the array B is empty. Consider each interval in array A. If the length of this interval is less than K, then ignore this interval. Otherwise, find the K-th largest number in this interval and add this number into array B.

In fact Alice doesn't care each element in the array B. She only wants to know the M-th largest element in the array B. Please help her to find this number.

Input
The first line is the number of test cases.

For each test case, the first line contains three positive numbers N(1≤N≤105),K(1≤K≤N),M. The second line contains N numbers Ai(1≤Ai≤109).

It's guaranteed that M is not greater than the length of the array B.

Output
For each test case, output a single line containing the M-th largest element in the array B.

Sample Input
2
5 3 2
2 3 1 5 4
3 3 1
5 8 2

Sample Output
3
2

• 点赞
• 写回答
• 关注问题
• 收藏
• 邀请回答

#### 3条回答默认 最新

• blownewbee 2019-08-08 23:01
已采纳
• Code_zx 2018-12-14 11:29

首先，将该数组进行降序排序（由大到小），则第K大的值就是下标为K-1的数组的数据，另外K值必须大于0，小于数组长度

点赞 打赏 评论
• 陶泥猴 2018-12-16 07:39
``````    int main()
{
int arr[10],k,i,j,temp;
for(i=0;i<10;i++)
scanf("%d",&arr[i]);
for(i=0;i<10;i++)
{
for(j=0;j<9;j++)
{
if(arr[j]<arr[j+1])
{
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
scanf("%d",&k);
printf("第%d大的数为%d\n",k,arr[k-1]);
return 0;
}

``````
点赞 打赏 评论