求助,只过了前两个样例,后三个wr

P1923 【深基9.例4】求第 k 小的数

ykl_705 @ 2024-07-20 11:41:03

#include<bits/stdc++.h>
#include<cstdio>
using namespace std;
int Qsort(int* a,int start,int end,int k){
    if(start == end && start == k-1)return a[start];
    else if(start < end){
        int i = start,j = end;
        int p = a[start];
        while(i < j){
            while(j > i && a[j] >= p)j--;
            a[i] = a[j];
            while(i < j && a[i] <= p)i++;
            a[j] = a[i];

        }
        a[i] = p;
        if(i == k-1)return a[i];
        else if(i < k-1)return Qsort(a,i + 1,end,k);
        else return Qsort(a,start,i - 1,k);
    }
}
int main(){
    int n,k;
    scanf("%d%d",&n,&k);
    int a[n];
    for(int i = 0;i < n;i++){
        scanf("%d",&a[i]);
    }
   printf("%d\n",Qsort(a,0,n - 1,k));
}

by abc1234shi @ 2024-07-20 12:07:25

@skd052232 不用这样,你用STL里的sort排序。


by abc1234shi @ 2024-07-20 12:07:54

@skd052232 然后输出a[k-1]就行了


by abc1234shi @ 2024-07-20 12:16:05

@skd052232 而且数组大小不能用n这个变量。求加团https://www.luogu.com.cn/team/83936


by ykl_705 @ 2024-07-20 13:00:09

@abc1234shi 想练一下分治,这样写函数不对吗qaq


by xunshen @ 2024-07-21 17:11:24

@abc1234shi 哪里可以直接用了


by xunshen @ 2024-07-21 17:11:55

@abc1234shi


by JackYTY @ 2024-08-15 09:42:33

@abc1234shi sort不能用,会TLE后两个点


|