跳到正文

个人记录

众数

mrh123456约 2 分钟
原文
文章信息与版本

文章信息

发布
2026年7月23日
收录
2026年7月23日
更新
2026年7月23日
篇幅
约 2 分钟

版本

  1. v1当前
C++
#include<bits/stdc++.h>
using namespace std;
int n;
struct node {
	long long a;   
	int s;     
};bool cmp(node c, node b) {
	return c.a < b.a;
}
node qqc[200007];
int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cin >> n;
	
	for (int i = 1; i <= n; i++) {
		cin >> qqc[i].a;
	}
	sort(qqc + 1, qqc + n + 1, cmp);
	vector<node> res; 
	for (int i = 1; i <= n; ) {
		int j = i;
		while (j <= n && qqc[j].a == qqc[i].a) {
			j++;
		}
		node temp;
		temp.a = qqc[i].a;
		temp.s = j - i;
		res.push_back(temp);
//		res.push_back({qqc[i].a, j - i});
		i = j;
	}
	int max_cnt = 0;
	for (int x=0;x<res.size();x++) {
		max_cnt = max(max_cnt, res[x].s);
	}
	for (int x=0;x<res.size();x++) {
		if (res[x].s == max_cnt) {
			cout << res[x].a << " " << res[x].s << '\n';
		}
	}
	
	return 0;
}/*题目描述
给定 n个 1 到 2×10 ^9之间的无序正整数,同一个正整数可能会出现多次。其中出现次数最多的整数称为众数。现在请你找出所有众数,并输出每个众数和它出现的次数。
输入格式
第一行:一个正整数 n(1≤n≤200000),表示给出的正整数的个数。
第二行:包含 n个正整数。
输出格式
按众数由小到大的顺序,每行输出两个数:
第 1 个:众数本身
第 2 个:该众数出现的次数
(如果有多个众数,都要输出)
*/

哪里出错了啊? 题目编号与名称:#6746. 众数来源:Becoder.com.cn

原文