40分#求救

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

Danielhaohao @ 2024-08-31 15:03:28

#include<bits/stdc++.h>
using namespace std;
const int N=5000010;
int a[N];
int main(){
    int n,k;
    scanf("%d%d",&n,&k);
    for(int i=1;i<=n;i++)scanf("%d",&a[i]);
    sort(a+1,a+n+1);
    cout<<a[k];
    return 0;
}

by Yxy7952 @ 2024-08-31 15:06:18

@Danielhaohao

#include<bits/stdc++.h>
using namespace std;
const int N=5000010;
int a[N];
int main(){
    int n,k;
    scanf("%d%d",&n,&k);
    for(int i=1;i<=n;i++)scanf("%d",&a[i]);
    sort(a+1,a+n+1);
    cout<<a[k];
    return 0;
}

by Yxy7952 @ 2024-08-31 15:07:03

@Danielhaohao

自己多调试一下,就不用发讨论区了


by XiaoYao789 @ 2024-08-31 15:10:21

@Danielhaohao 用nth_elepment(a,a+1,a+n+1)排序就可以了


by XiaoYao789 @ 2024-08-31 15:10:58

发错了,是nth_element(a,a+1,a+n+1)


by Yxy7952 @ 2024-08-31 15:29:56

@yixingyou

哦,发错了,但很简单的错 最后输出 a[k+1] 就行了,没必要这么麻烦,而且是我先发的


by ZHAOJJzjj123 @ 2024-09-17 13:17:33

@Yxy7952 您应该写k+1,因为题目说了最小的是第0个


by Yxy7952 @ 2024-09-18 21:23:25

@ZHAOJJzjj123

请看回复,懒得删

希望看完帖子再说话,都18天前的了


|