题解 | #中位数#

中位数

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

#include <iostream>
#include <algorithm>

using namespace std;

const int N = 1e5 + 10;


int main(){
	int n;
	while(cin >> n){
		if(n == 0)break; 
		int a[N];
		for(int i = 0;i < n;i ++)cin >> a[i];
		
		sort(a,a + n);
		if(n % 2 != 0){
			cout << a[n / 2] << endl;	
		}else{
			cout << (a[n / 2 - 1] + a[n / 2]) / 2 << endl;
		}
	}
	return 0;
}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

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