[cpp] map

김세훈·2023년 8월 21일
0

cpp

목록 보기
1/1

#include <iostream>
// map 헤더 선언
#include <map>
#include <string>
using namespace std;

int main()
{
    string str;
    // 여기서 char형식이 아닌 string형식을 사용해서 count가 안됐음
    // map 선언
    map<char, int> tmp;
    cin >> str;
    for (int i = 0; i < str.length(); i++)
    {
        // 입력된 알파벳을 대문자로 변환
        str[i] = toupper(str[i]);
        if (tmp.count(str[i]) == 1)
        {
            // value값 증가
            tmp[str[i]] += 1;
        }
        else
        {
            // key, value 삽입
            tmp[str[i]] = 1;
        }
    }
    int n = -1;
    char c;
    // pair변수를 tmp와 같은 형식으로 선언
    for (const auto &pair : tmp)
    {
        // pair의 value
        if (pair.second > n)
        {
            n = pair.second;
            c = pair.first;
        }
    }
    int cnt = 0;
    
    // 같은 value값을 탐색하는 코드
    for (const auto &pair : tmp)
    {
        if (pair.second == n)
        {
            cnt++;
        }
    }
    if (cnt > 1)
    {
        cout << '?';
    }
    else
    {
        cout << c;
    }
}

0개의 댓글

관련 채용 정보