본문 바로가기
개발/Coding Test

[프로그래머스 C++ Lv2] 스킬트리

by Eunduck 2022. 6. 6.
728x90

안녕하십니까. Eun🦆입니다.

풀이법 입니다.

 

 

코딩테스트 연습 - 스킬트리

 

programmers.co.kr

#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
// 파라미터로 주어지는 문자열은 const로 주어집니다. 변경하려면 문자열을 복사해서 사용하세요.
// skill_trees_len은 배열 skill_trees의 길이입니다.
int solution(const char* skill, const char* skill_trees[], size_t skill_trees_len) {
    int answer = 0;
    for(int i=0;i<skill_trees_len;i++) // skill_trees 모두 탐색
    {
        int cnt = 0;
        int error = 0; // 에러 판별
        for(int j=0;j<strlen(skill_trees[i]);j++) // skill_trees[i] 글자 탐색
        {
            if(error) // 에러 판단시 다음 글자로
                break;
            for(int k=0;k<strlen(skill);k++) // skill 뒤부터 D, B, C 탐색
            {
                //printf("#i=%d, j=%c, k=%c\n",i, skill_trees[i][j], skill[k]);
                if(skill_trees[i][j] == skill[k])
                {
                    //printf("#i=%d, j=%c, k=%c\n",i, skill_trees[i][j], skill[k]);
                    if(skill_trees[i][j] == skill[cnt]) // skill 처음 변수 찾은 경우
                    {
                        printf("match! i=%d, j=%d, k=%d, skill_trees[%d][%d]=%c, skill[%d]=%c\n",i,j,k,i,j,skill_trees[i][j],cnt,skill[cnt]);
                        cnt++;
                    }
                    else
                    {
                        printf("error! i=%d, j=%d, k=%d, skill_trees[%d][%d]=%c, skill[%d]=%c\n",i,j,k,i,j,skill_trees[i][j],cnt,skill[cnt]);
                        error = 1;
                        break;
                    }
                }                
            }
            if(j==strlen(skill_trees[i])-1 & error==0)
            {
                answer++;
                printf("no error! answer = %d\n",answer);
            }
        }        
    }
    return answer;
}

 

네이버 블로그 리뉴얼입니다.

(https://blog.naver.com/unsuk1/221936853488)

728x90

댓글