사자자리
[C++] 백준 1157번: 단어 공부 본문
https://www.acmicpc.net/problem/1157
1157번: 단어 공부
알파벳 대소문자로 된 단어가 주어지면, 이 단어에서 가장 많이 사용된 알파벳이 무엇인지 알아내는 프로그램을 작성하시오. 단, 대문자와 소문자를 구분하지 않는다.
www.acmicpc.net
#include <iostream>
using namespace std;
int main() {
int alphabet[26] = {0}, max = 0, max_alpha, max_count = 0;
string word;
cin >> word;
for (int i = 0; i < word.length(); i++) { //ASCII 코드를 사용하여 대소문자 문제 해결
if (word[i] < 97) alphabet[word[i] - 65]++; //대문자일 때
else alphabet[word[i] - 97]++; //소문자일 때
}
for (int i = 0; i < 26; i++) { //가장 많이 사용된 알파벳 찾기
if (alphabet[i] > max) {
max = alphabet[i];
max_alpha = i;
}
}
for (int i = 0; i < 26; i++) { //가장 많이 사용된 알파벳의 개수 찾기
if (max == alphabet[i]) max_count++;
}
if (max_count == 1) cout << (char)(max_alpha + 65); //가장 많이 사용된 알파벳이 1개인 경우
else cout << "?";
return 0;
}
'C++ > C++ 문제' 카테고리의 다른 글
[C++] 백준 4673번: 셀프 넘버 (0) | 2022.08.03 |
---|---|
[C++] 백준 10872번: 팩토리얼 (0) | 2022.08.03 |
[C++] 백준 10871번: X보다 작은 수 (0) | 2022.07.27 |
[C++] 백준 2739번: 구구단 (0) | 2022.07.27 |
[C++] 백준 8958번: OX퀴즈 (0) | 2022.07.16 |
Comments