用unordered_map求元素出现次数最多的那个,为什么不能全部通过呢?
频繁元素
https://ac.nowcoder.com/acm/problem/22229
#include <unordered_map> using namespace std; int main(){ int n, number; scanf("%d", &n); if (n < 1||n>20) return 0; unordered_map<int, int> ump; for (int i = 0; i <n; i++) { scanf("%d", &number); ump[number] ++; } int maxFreq=-1,maxNum=-100000; for (unordered_map<int, int>::iterator it = ump.begin(); it != ump.end(); it++) { if ((it->second) > maxFreq) { maxFreq = it->second; maxNum = it->first; } } printf("%d\n",maxNum); return 0; }