문제 출처:https://www.acmicpc.net/problem/5622
5622번: 다이얼
첫째 줄에 알파벳 대문자로 이루어진 단어가 주어진다. 단어의 길이는 2보다 크거나 같고, 15보다 작거나 같다.
www.acmicpc.net

문제 분석
그림과 같이 ABC는 2, BCD는 3 ... 9까지 표현이 가능합니다. 알파벳이 주어질 때 숫자로 변환하고 다이얼을 걸기 위한 최소 시간을 출력하면 됩니다. 1이라는 숫자를 다이얼로 표기하기 위해서는 2초가 필요하고 숫자가 1추가될 때마다 1초가 추가되므로 2를 누르기 위해서는 3초, 3은 4초, ... 9는 10초가 걸리게 됩니다. 이 점을 생각하면서 문제를 풀면 어려움없이 해결할 수 있습니다. 코드는 다음과 같습니다:
#include <iostream>
using namespace std ;
int main(){
ios::sync_with_stdio(false) ;
cin.tie(NULL) ; cout.tie(NULL) ;
while(true){
string str ;
int vowel_cnt = 0, consonant_cnt = 0 ;
bool exist_vowel = false, answer = true ;
char prev = ' ' ;
cin >> str ;
if(str.compare("end") == 0)
break ;
for(auto c : str){
if(c != 'e' && c != 'o' && prev == c){
answer = false ;
break;
}
switch(c){
case 'a': case 'e': case 'i': case 'o': case 'u':
exist_vowel = true ;
vowel_cnt += 1 ;
consonant_cnt = 0 ;
break ;
default :
consonant_cnt += 1 ;
vowel_cnt = 0 ;
break ;
}
if(2 < vowel_cnt || 2 < consonant_cnt){
answer = false ;
break;
}
prev = c;
}
if(!exist_vowel)
answer = false ;
cout << "<" << str << "> is" << (answer ? " " : " not ") << "acceptable." << endl ;
}
return 0 ;
}
해당 문제는 Github에서도 보실 수 있습니다:
https://github.com/gurcks8989/CodingTest/blob/master/BackJoon/HPS/P5622_Dial.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/5622
5622번: 다이얼
첫째 줄에 알파벳 대문자로 이루어진 단어가 주어진다. 단어의 길이는 2보다 크거나 같고, 15보다 작거나 같다.
www.acmicpc.net

문제 분석
그림과 같이 ABC는 2, BCD는 3 ... 9까지 표현이 가능합니다. 알파벳이 주어질 때 숫자로 변환하고 다이얼을 걸기 위한 최소 시간을 출력하면 됩니다. 1이라는 숫자를 다이얼로 표기하기 위해서는 2초가 필요하고 숫자가 1추가될 때마다 1초가 추가되므로 2를 누르기 위해서는 3초, 3은 4초, ... 9는 10초가 걸리게 됩니다. 이 점을 생각하면서 문제를 풀면 어려움없이 해결할 수 있습니다. 코드는 다음과 같습니다:
#include <iostream>
using namespace std ;
int main(){
ios::sync_with_stdio(false) ;
cin.tie(NULL) ; cout.tie(NULL) ;
while(true){
string str ;
int vowel_cnt = 0, consonant_cnt = 0 ;
bool exist_vowel = false, answer = true ;
char prev = ' ' ;
cin >> str ;
if(str.compare("end") == 0)
break ;
for(auto c : str){
if(c != 'e' && c != 'o' && prev == c){
answer = false ;
break;
}
switch(c){
case 'a': case 'e': case 'i': case 'o': case 'u':
exist_vowel = true ;
vowel_cnt += 1 ;
consonant_cnt = 0 ;
break ;
default :
consonant_cnt += 1 ;
vowel_cnt = 0 ;
break ;
}
if(2 < vowel_cnt || 2 < consonant_cnt){
answer = false ;
break;
}
prev = c;
}
if(!exist_vowel)
answer = false ;
cout << "<" << str << "> is" << (answer ? " " : " not ") << "acceptable." << endl ;
}
return 0 ;
}
해당 문제는 Github에서도 보실 수 있습니다:
https://github.com/gurcks8989/CodingTest/blob/master/BackJoon/HPS/P5622_Dial.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
훈수 및 조언은 언제든 환영입니다.