CCF-CSP 201312-1 The most frequent occurrences

Problem description
  Given n positive integers, find out the most frequent ones among them. If there are multiple such numbers, please output the smallest one.
Input format
  The first line of input only has a positive integer n (1 ≤ n ≤ 1000), indicating the number of digits.
  The second line of the input has n integers s1, s2,…, sn (1 ≤ si ≤ 10000, 1 ≤ i ≤ n). Adjacent numbers are separated by spaces.
Output format
  Output the number of occurrences among the n times. If there are multiple such numbers, the smallest one is output.
Sample input
6
10 1 10 20 30 20
Sample output
10

Summary of experience:
Use map to install numbers and their occurrence times, and then traverse the map to find the qualified numbers.

C ++ code:

#include<bits/stdc++.h>
using namespace std;
int main() {
	int n,max = 0,best;
	map<int,int> m;
	scanf("%d",&n);
	while(n--){
		int a;
		scanf("%d",&a);
		m[a]++;
	}
	for(auto i:m){
		if(max<i.second){
			best = i.first;
			max = i.second;
		}
	}
	printf("%d",best);
	return 0;
}
Published 111 original articles · won praise 2 · Views 3533

Guess you like

Origin blog.csdn.net/m0_38088647/article/details/100517100