题解 | #中位数#

中位数

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

#include <iostream>
#include <algorithm>

using namespace std;

const int MAXN = 10000 + 10;

int arr[MAXN];

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

全部评论

相关推荐

05-16 11:16
已编辑
东华理工大学 Java
牛客73769814...:盲猜几十人小公司,庙小妖风大,咋不叫她去4️⃣呢😁
牛客创作赏金赛
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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