???

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

Xiaonao_Dali @ 2023-12-30 21:38:07

#include<bits/stdc++.h>
using namespace std;
bool cmp(int a,int b){
    return a<b;
}
int main(){
    int n,a[10005],k;
    scanf("%d%d",&n,&k);
    for(int i=0;i<n;i++){
        scanf("%d",&a[i]);
    }
    sort(a,a+n);
    int len=unique(a,a+n)-a;
    if(k<len){
        printf("%d",a[k]);
    }
    else if(k>len){
        printf("NO RESULT");
    }
    return 0;
}

by I_will_AKIOI @ 2023-12-30 21:42:23

a 数组开 10^4 怎么能过?开大后放外面。去重删掉。


by I_will_AKIOI @ 2023-12-30 21:43:16

#include<bits/stdc++.h>
using namespace std;
int n,a[5000005],k;
bool cmp(int a,int b){
    return a<b;
}
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;
}

by sulingfeng @ 2024-01-16 18:45:03

不用写cmp


by sulingfeng @ 2024-01-16 18:45:40

也不用写if else


by sulingfeng @ 2024-01-16 18:46:32

len也不需要呀


|