목차

  1. 환경
  2. 문제
  3. 접근법
  4. Library
  5. Code
  6. 정리
  7. Link

1. 환경

Editor: Microsoft Visual Studio Code
Compiler: C++20

2. 문제

Link1 BOJ1157

3. 접근법

알고리즘 분류

  • 구현
  • 문자열

소문자, 대문자를 구분하여 알파벳 등장 개수를 저장하는 배열에 저장한다.
이후 최댓값을 골라 대문자로 출력한다.

4. Library

bits/stdc++.h - Link2
(GNU C++ Standard Library Header - X)
(GCC Compiler Header - O)

5. Code

#include <bits/stdc++.h>

using namespace std;

int arr[26];
char res;
bool flag;
string str;

void input();
void func();
void output();

int main(void)
{
    input();
    func();
    output();
}

void input()
{
    cin >> str;
}

void func()
{
    int maxval = 0, idx;
    for(int i = 0; i < str.size(); i++)
    {
        if(str.at(i) <= 'Z')
            arr[str.at(i) - 'A']++;
        else
            arr[str.at(i) - 'a']++;
    }
    for(int i = 0; i < 26; i++)
    {
        if(maxval < arr[i])
        {
            maxval = arr[i];
            idx = i;
            flag = false;
        }
        else if(maxval == arr[i])
            flag = true;
    }
    res = idx + 'A';
}

void output()
{
    if(flag)
        cout << "?\n";
    else
        cout << res << '\n';
}

6. 정리

문자열과 최댓값을 찾는 알고리즘을 공부할 수 있었다.
추가적으로 ASCII code에서 대문자가 소문자보다 작은 값을 갖는다.

1. BOJ1157 - 단어 공부
https://www.acmicpc.net/problem/1157
2. bits/stdc++.h
https://gcc.gnu.org/onlinedocs/gcc-4.8.0/libstdc++/api/a01541_source.html
3. Cplusplus.com
https://www.cplusplus.com/reference/