题解 | #中位数#

中位数

https://www.nowcoder.com/practice/2364ff2463984f09904170cf6f67f69a

//知道sort后这题就简单多了,而且还是O(log2N)的复杂度

#include "stdio.h"

#include "algorithm"

using namespace std;

int main(){

    int N;int num[10001];

    while (scanf("%d",&N)!=EOF) {

        if(N == 0)

        return 0;

        for (int i =0; i<N; ++i) {

            scanf("%d",num+i);

        }

        sort(num,num+N);

        if(N%2 != 0)

        printf("%d\n",num[N/2]);

        else{

            printf("%d\n",(num[N/2]+num[N/2-1])/2);

        }

    }

}

全部评论

相关推荐

不愿透露姓名的神秘牛友
06-11 13:34
offe从四面八方来:我真的没时间陪你闹了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务