高级代码,需要的评论个好评

LJY  •  1年前


#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; 

 


评论:

6


张黄源  •  1年前
17145254891665.png

牛延希  •  1年前

11451411037253星好评


荀逸萱  •  1年前

#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; 
} z坐坐坐坐下


刘焕群  •  1年前