[프로그래머스/C++] 코드 처리하기

꿈별·2023년 7월 28일
0

문제풀이

목록 보기
18/52

문제


풀이

#include <string>

using namespace std;

string solution(string code) {
    bool mode = 0;
    //char one = 0x31; // 1
    string ret = "";
    for (int idx = 0; idx < code.length(); idx++)
    {
        if (0 == mode)
        {
            if ('1' == code[idx])
                mode = 1;
            else
                if (0 == idx % 2)
                    ret += code[idx];
        }
        else
        {
            if ('1' == code[idx])
                mode = 0;
            else
                if (1 == idx % 2)
                    ret += code[idx];
        }
    }
    if ("" == ret)
        return "EMPTY";
    return ret;
}

  • 실수
  1. 문제를 제대로 읽자
    : 짝수라는 설명만 보고 냅다 0 == idx일 경우를 제외시켜 버렸다.
    예시를 보니 0일 때도 짝수로 보고 출력하고 있었다..

  2. 문자에 작은 따옴표 붙이기
    : 1 == code[idx]가 아니라'1' == code[idx] 가 맞다.

0개의 댓글