#9060.「力扣169」多数元素 题解 审核通过

CPP 刷题王 2025-07-13 11:50:40 7

结论题。

直接排序输出第 个位置的元素即可。因为众数一定经过这个位置,应该很显然吧。。

CODE:

#include <bits/stdc++.h>
using namespace std;
#define int long long
int a[50010];
signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);
	int n;
	cin >> n;
	for (int i = 1; i <= n; i++) {
		cin >> a[i];
	}
	sort(a + 1, a + 1 + n);
	cout << a[(int)ceil(n / 2.0)];	
	return 0;
}
{{ vote && vote.total.up }}