https://www.acmicpc.net/problem/1302
#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
using namespace std;
int n, max_=0;
map<string, int> m;
map<string, int>::iterator iter;
string str;
int main(){
cin>>n;
for(int i=0 ; i<n;i++){
cin >> str;
m[str]++; // 딕셔너리 형태로 업카운트
}
// for (iter=m.begin(); iter!=m.end(); iter++){
// cout<< iter->first <<" "<< iter->second << endl;
// }
for (iter=m.begin(); iter!=m.end();iter++){
max_ = max(max_, iter->second);
}
for(iter=m.begin(); iter!= m.end(); iter++){
if (iter->second == max_){
cout<<iter->first;
break;
}
}
}
- 파이썬의 딕셔너리 형태와 유사한 map 자료구조를 사용
- 입력이 들어올때마다 해당 키값에 해당하는 second를 카운트업
- 다시 반복문으로 가장 큰 second값을 찾고
- 또 다시 반복문으로 그 second값에 해당하는 first값을 출력한 후 break로 프로그램 종료
'자기개발👨💻 > 코딩 알고리즘' 카테고리의 다른 글
[C++] 백준 11279최대힙, 1927최소힙 (0) | 2022.03.18 |
---|---|
[C++] 백준 1158 요세푸스 문제 //<queue> (0) | 2022.03.18 |
[C++] 백준 11651 // <vector><map> (0) | 2022.03.16 |
[C++] 백준 9012 괄호 (알고스터디 E조) (0) | 2022.03.15 |
[C++] 프로그래머스 더 맵게 (0) | 2022.03.13 |
[C++] 백준 1448 삼각형 만들기 (0) | 2022.02.28 |