Run ID 作者 问题 语言 测评结果 Time Memory 代码长度 提交时间
104584 张永良 19众数 C++ Accepted 6 MS 312 KB 808 2025-01-05 14:29:49

Tests(11/11):


Code:

#include <iostream> #include <unordered_map> #include <vector> #include <algorithm> using namespace std; int main() { int n; cin >> n; unordered_map<int, int> countMap; // 读取数组并统计每个元素出现的次数 for (int i = 0; i < n; ++i) { int num; cin >> num; countMap[num]++; } // 找出出现次数最多的数,如果有多个众数,选择最小的 int mostFrequent = -1; int maxCount = 0; for (auto& pair : countMap) { if (pair.second > maxCount || (pair.second == maxCount && pair.first < mostFrequent)) { mostFrequent = pair.first; maxCount = pair.second; } } // 输出结果 cout << mostFrequent << endl; return 0; }