문제 출처:https://www.acmicpc.net/problem/1157
1157번: 단어 공부
알파벳 대소문자로 된 단어가 주어지면, 이 단어에서 가장 많이 사용된 알파벳이 무엇인지 알아내는 프로그램을 작성하시오. 단, 대문자와 소문자를 구분하지 않는다.
www.acmicpc.net

문제 분석
우선 문자열을 각각 count하는 부분이 필요합니다. 또한, count된 알파벳들의 maximum 값과 그 값이 중복인지를 확인해야합니다. 코드는 다음과 같습니다:
#include <iostream>
#define ALPHABET 26
using namespace std ;
int main(){
ios::sync_with_stdio(false) ;
cin.tie(NULL) ; cout.tie(NULL) ;
int cnt[ALPHABET] = {0, }, max = 0, index ;
bool is_duplication = false ;
string word ;
cin >> word ;
for(auto a : word){
if('Z' < a) // lowercase
cnt[a - 'a'] += 1 ;
else
cnt[a - 'A'] += 1 ;
}
for(int i = 0 ; i < ALPHABET ; i++){
if(max < cnt[i]){
index = i ;
max = cnt[i] ;
is_duplication = false ;
}
else if(max == cnt[i])
is_duplication = true ;
}
char answer = is_duplication ? '?' : 'A' + index ;
cout << answer << endl ;
return 0 ;
}
해당 문제는 Github에서도 보실 수 있습니다:
https://github.com/gurcks8989/CodingTest/blob/master/BackJoon/HPS/P1157_Study_Word.cpp
GitHub - gurcks8989/CodingTest: CodingTest_study_with_c++
CodingTest_study_with_c++. Contribute to gurcks8989/CodingTest development by creating an account on GitHub.
github.com
훈수 및 조언은 언제든 환영입니다.
문제 출처:https://www.acmicpc.net/problem/1157
1157번: 단어 공부
알파벳 대소문자로 된 단어가 주어지면, 이 단어에서 가장 많이 사용된 알파벳이 무엇인지 알아내는 프로그램을 작성하시오. 단, 대문자와 소문자를 구분하지 않는다.
www.acmicpc.net

문제 분석
우선 문자열을 각각 count하는 부분이 필요합니다. 또한, count된 알파벳들의 maximum 값과 그 값이 중복인지를 확인해야합니다. 코드는 다음과 같습니다:
#include <iostream>
#define ALPHABET 26
using namespace std ;
int main(){
ios::sync_with_stdio(false) ;
cin.tie(NULL) ; cout.tie(NULL) ;
int cnt[ALPHABET] = {0, }, max = 0, index ;
bool is_duplication = false ;
string word ;
cin >> word ;
for(auto a : word){
if('Z' < a) // lowercase
cnt[a - 'a'] += 1 ;
else
cnt[a - 'A'] += 1 ;
}
for(int i = 0 ; i < ALPHABET ; i++){
if(max < cnt[i]){
index = i ;
max = cnt[i] ;
is_duplication = false ;
}
else if(max == cnt[i])
is_duplication = true ;
}
char answer = is_duplication ? '?' : 'A' + index ;
cout << answer << endl ;
return 0 ;
}
해당 문제는 Github에서도 보실 수 있습니다:
https://github.com/gurcks8989/CodingTest/blob/master/BackJoon/HPS/P1157_Study_Word.cpp
GitHub - gurcks8989/CodingTest: CodingTest_study_with_c++
CodingTest_study_with_c++. Contribute to gurcks8989/CodingTest development by creating an account on GitHub.
github.com
훈수 및 조언은 언제든 환영입니다.