반응형
프로그래머스 코딩 테스트 문제 "옹알이 (1)"을 풀어 본다.
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
char *basic[] = { "aya", "ye", "woo", "ma" };
bool guess(char* words) {
bool used[] = { false, false, false, false };
int length = strlen(words);
int ptr = 0;
do {
bool match = false;
for(int i = 0;i < 4;i++) {
if(used[i] == false) {
if(strncmp(&words[ptr], basic[i], strlen(basic[i])) == 0) {
used[i] = true;
match = true;
ptr += strlen(basic[i]);
break;
}
}
}
if(match == false) {
return false;
}
}
while(ptr < length);
return true;
}
// babbling_len은 배열 babbling의 길이입니다.
// 파라미터로 주어지는 문자열은 const로 주어집니다. 변경하려면 문자열을 복사해서 사용하세요.
int solution(const char* babbling[], size_t babbling_len) {
int answer = 0;
for(int i = 0;i < babbling_len;i++) {
if(guess(babbling[i]) == true) {
answer++;
}
}
return answer;
}
주어진 문자열에 대하여 머쓱이가 발음할 수 있는 문자열을 처음부터 비교하여 최종 종료될 때까지 반복한다.
반응형



