[CCF201503-2] Number sorting (compilation error)

Problem description
  Given n integers, please count the number of occurrences of each integer and output them in the order of the number of occurrences.
Input format
  The first line of input contains an integer n, which represents the number of given numbers.
  The second line contains n integers, and adjacent integers are separated by a space to indicate the given integer.
Output format
  outputs multiple lines, each line contains two integers, respectively representing a given integer and the number of times it appears. Output in descending order of occurrence. If two integers appear the same number of times, the smaller value is output first, and then the larger value is output.
Sample input

12
5 2 3 3 1 3 4 2 5 2 3 5

Sample output

3 4
2 3
5 3
1 1
4 1

Evaluation use case scale and conventions

1 ≤ n ≤ 1000, the numbers given are all non-negative integers not exceeding 1000.

What kind of operation is this? Which era of compiler is used? Well, I have been searching and finding it. Anyway, the method is fine. . This, served. Changed, right .

Insert picture description here

Compilation error
#include <iostream>
#include <vector>
#include <map>
#include <algorithm>
using namespace std;

bool cmp(pair<int,int> p1,pair<int,int> p2){
    
    
	if(p1.second!=p2.second)
		return p1.second>p2.second;
	else
		return p1.first<p2.first;
}

int main(){
    
    
	int n,num;
	cin >> n;
	map<int,int> m;
	vector<pair<int,int>> v;
	for(int i = 0;i<n;i++){
    
    
		cin >> num;
		m[num]++;
	}
	for(auto it = m.begin();it!=m.end();it++){
    
    
		v.push_back(make_pair(it->first,it->second));
	}
	sort(v.begin(),v.end(),cmp);
	for(auto it = v.begin();it!=v.end();it++){
    
    
		cout << it->first << " " << it->second << endl;
	}
	return 0;
}
AC code
#include <iostream>
#include <vector>
#include <map>
#include <algorithm>
using namespace std;

bool cmp(pair<int,int> p1,pair<int,int> p2){
    
    
	if(p1.second!=p2.second)
		return p1.second>p2.second;
	else
		return p1.first<p2.first;
}

int main(){
    
    
	int n,num;
	cin >> n;
	map<int,int> m;
	for(int i = 0;i<n;i++){
    
    
		cin >> num;
		m[num]++;
	}
	vector<pair<int,int> > v(m.begin(),m.end()); 
	sort(v.begin(),v.end(),cmp);
	for(int i = 0;i<v.size();i++){
    
    
        cout << v[i].first << " " << v[i].second << endl;
    }
	return 0;
}

Guess you like

Origin blog.csdn.net/weixin_45845039/article/details/109287722