https://www.acmicpc.net/problem/1157
가장 많이 사용된 알파벳을 알아내면 되는 문제이다.
이 문제는 간단하게 map자료형을 사용하여 각 알파벳의 사용횟수를 세고 그 map을 순회하며 만약 최대값이 같은 알파벳이 있다면 ? 를 반환하게 해주면 된다.
정답코드
#include "iostream"
#include "string"
#include "map"
using namespace std;
int main()
{
string s;
map<char, int> sm;
cin >> s;
for (int i = 0; i < s.length(); i++)
{
if (islower(s[i])) sm[toupper(s[i])]++;
else sm[s[i]]++;
}
int maxcnt = 0;
char mosts = '?';
for (auto& pair : sm)
{
if (pair.second > maxcnt)
{
maxcnt = pair.second;
mosts = pair.first;
}
else if (pair.second == maxcnt) {
mosts = '?';
}
}
cout << mosts << endl;
}
'코딩테스트 > 백준' 카테고리의 다른 글
[백준][C++]9506번. 약수들의 합 (0) | 2024.09.02 |
---|---|
[백준][C++]2869번. 달팽이는 올라가고 싶다. (0) | 2024.08.30 |
[백준][C++]2941번. 크로아티아 알파벳 (0) | 2024.08.29 |
[백준][C++]25206번. 너의 평점은 (0) | 2024.08.29 |
[백준][C++]1316번. 그룹 단어 체커 (0) | 2024.08.28 |