문제 링크입니다: https://programmers.co.kr/learn/courses/30/lessons/84512
코딩테스트 연습 - 5주차
사전에 알파벳 모음 'A', 'E', 'I', 'O', 'U'만을 사용하여 만들 수 있는, 길이 5 이하의 모든 단어가 수록되어 있습니다. 사전에서 첫 번째 단어는 "A"이고, 그다음은 "AA"이며, 마지막 단어는 "UUUUU"입니
programmers.co.kr
word의 길이가 비교적 짧으므로 모든 경우의 수를 구한 뒤 순서를 출력해주면 되는 문제였습니다.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <string> | |
#include <vector> | |
#include <algorithm> | |
using namespace std; | |
vector<char> vowels = { 'A', 'E', 'I', 'O', 'U' }; | |
vector<string> dictionary; | |
void func(string word, int wordLength) | |
{ | |
if (wordLength == word.length()) | |
{ | |
dictionary.push_back(word); | |
return; | |
} | |
for (char vowel : vowels) | |
{ | |
func(word + vowel, wordLength); | |
} | |
} | |
int solution(string word) { | |
for (int len = 1; len <= 5; len++) | |
{ | |
string word = ""; | |
func(word, len); | |
} | |
sort(dictionary.begin(), dictionary.end()); | |
for (int i = 0; i < dictionary.size(); i++) | |
{ | |
if (word == dictionary[i]) | |
{ | |
return i + 1; | |
} | |
} | |
} | |
int main(void) | |
{ | |
cout << solution("I") << "\n"; | |
return 0; | |
} |

개발환경:Visual Studio 2017
지적, 조언, 질문 환영입니다! 댓글 남겨주세요~
반응형
'알고리즘 > programmers' 카테고리의 다른 글
[Programmers 코딩테스트 고득점 Kit] 타겟 넘버 (0) | 2021.09.07 |
---|---|
[Programmers 위클리 챌린지 6주차] 복서 정렬하기 (0) | 2021.09.06 |
[Programmers 위클리 챌린지 4주차] 직업군 추천하기 (0) | 2021.09.04 |
[Programmers 위클리 챌린지 3주차] 퍼즐 조각 채우기 (0) | 2021.09.04 |
[Programmers 위클리 챌린지 2주차] 상호 평가 (0) | 2021.09.03 |