백준 문제풀이/브론즈1
백준 1157번 - 단어 공부
void_melody
2021. 10. 28. 00:56
https://www.acmicpc.net/problem/1157
1157번: 단어 공부
알파벳 대소문자로 된 단어가 주어지면, 이 단어에서 가장 많이 사용된 알파벳이 무엇인지 알아내는 프로그램을 작성하시오. 단, 대문자와 소문자를 구분하지 않는다.
www.acmicpc.net
사고과정)
문자열들을 입력받고, 문자열의 알파벳 갯수에 따라 알파벳순으로 정렬한 배열을 만든다.
int array[26] = {0}으로 초기화하고, 갯수들을 넣어주면 됨.
그런다음, 가장 많은 갯수에 해당하는 알파벳을 찾고 (여기선 *max_element를 활용)
가장 많은 갯수가 중복되면, ?을 출력. 아니면 그냥 해당 알파벳을 출력해주면 된다.
#include <iostream>
#include <string>
#include <algorithm>
#define NUM 26
using namespace std;
int main()
{
string input;
cin >> input;
int array[NUM] = { 0, };
int length = input.length();
for (int i = 0; i < length; i++)
{
if ('A' <= input[i] && input[i] <= 'Z') // Big
array[input[i] - 'A']++;
else if ('a' <= input[i] && input[i] <= 'z') // small
array[input[i] - 'a']++;
}
int max = *max_element(array, array + NUM);
int count = 0; int index = 0;
for (int i = 0; i < NUM; i++)
{
if (array[i] == max)
{
count++;
index = i;
}
}
if (count != 1)
{
cout << '?';
return 0;
}
else
{
cout << char(index + 'A');
return 0;
}
}