안녕 세상아,

[c++/백준] 11652 카드 본문

백준

[c++/백준] 11652 카드

돈 많은 백수가 되고싶다 2023. 8. 28. 13:06

https://www.acmicpc.net/problem/11652

 

11652번: 카드

준규는 숫자 카드 N장을 가지고 있다. 숫자 카드에는 정수가 하나 적혀있는데, 적혀있는 수는 -262보다 크거나 같고, 262보다 작거나 같다. 준규가 가지고 있는 카드가 주어졌을 때, 가장 많이 가지

www.acmicpc.net

map 함수를 이용하여 풀었다. 

map 함수를 완성시킨 후 vector에 옮겨서 정렬하였다. 

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

bool cmp(pair<long long int, int> a, pair<long long int, int> b) {
	//가장 많이 가지고 있는 정수 여러가지일 때 작은 것 출력
	if (a.second == b.second) {
		return a.first < b.first;
	}
	//가장 많이 가지고 있는 정수 구하기
	return a.second > b.second;
}
int main() {
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	
	long long int n;
	cin >> n;

	map<long long int, int> m;
	for (int i = 0; i < n; i++) {
		long long int x;
		cin >> x;

		m[x]++;
	}
	vector<pair<long long int, int>> v(m.begin(), m.end());

	sort(v.begin(), v.end(), cmp);

	cout << v[0].first;
}

맵 해시 공부중인데 쉽지않군

'백준' 카테고리의 다른 글

[백준/c++] 2178 미로 탐색  (1) 2024.11.16
[c++/백준] 11722 가장 긴 감소하는 부분 수열  (0) 2023.08.30
[c++/백준] 9659 돌 게임 5  (0) 2023.08.18
[c++/백준] 21921 블로그  (0) 2023.08.09
[c++/백준] 11051 이항 계수 2  (0) 2023.06.28