Zzy20060323 @ 2024-09-22 17:56:57
#include<stdio.h>
int n, k;
int a[5000001];
void swap(int *x, int *y)
{
int tmp = *x;
*x = *y;
*y = tmp;
}
void px(int begin,int end)
{
if (begin >= end)
{
return;
}
int right = end;
int left = begin;
int bj = left;
while (left < right)
{
while (left<right&&a[right] >= a[bj])
{
right--;
}
while (left<right&&a[left] < a[bj])
{
left++;
}
swap(&a[left], &a[right]);
}
swap(&a[bj], &a[left]);
if (k < bj)
{
px(begin, bj - 1);
}
else if (k > bj)
{
px(bj + 1, end);
}
}
int main()
{
scanf("%d%d", &n, &k);
for (int i = 0; i < n; i++)
{
scanf("%d", &a[i]);
}
px(0, n-1);
printf("%d", a[k]);
return 0;
}
by wangtianhao0609 @ 2024-09-23 22:21:57
不用那么复杂:
#include<bits/stdc++.h>
using namespace std;
int n,k,a[5000001];
int main() {
scanf("%d%d",&n,&k);
for(int i=0;i<=n;i++)
{
scanf("%d",&a[i]);
}
sort(a,a+n);
printf("%d",a[k]);
return 0;
}