본문 바로가기
C++

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

by 띰쥬 2025. 8. 28.
728x90
반응형
SMALL

#include <string>
#include <vector>

using namespace std;

string solution(string code) {
    string ret = "";
    //mode 0 : code[idx] !=1 / idx가 짝수일때만 ret 맨뒤 code[idx]추가
    //mode 0 : code[idx] ==1 / mode0을 1로 변경
    //mode 1 : code[idx] !=1 / idx가 홀수일때만 ret 맨뒤 code[idx]추가
    //mode 1 : code[idx] ==1 / mode1을 0로 변경
    
    bool mode = false;
    
    //0~code의 길이-1 까지 반복
    for(int idx=0; idx<code.length(); idx++)
    {
        if(!mode)
        {
            if(code[idx] != '1')
            {
                if(idx%2==0)
                    ret += code[idx];
            }
            else if((int)code[idx])
                mode = true;
        }
        else
        {
            if(code[idx] != '1')
            {
                if(idx%2==1)
                    ret += code[idx];
            }
            else if((int)code[idx])
                mode = false;
        }
    }
    
    if(ret.length() == 0)
        return "EMPTY";
    
    return ret;
}

728x90
반응형
LIST

댓글