공부/C++

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

굴러다니다니 2025. 4. 21. 17:42

https://school.programmers.co.kr/learn/courses/30/lessons/181932?language=cpp

#include <string>
#include <vector>
#include <iostream>

using namespace std;

string solution(string code) {
    string answer = "";
    int mode = 0;
    for(int i = 0; i < code.length(); i++){
        if(code[i] == '1') mode = ++mode % 2;
        else {
            if(mode == 0 && i%2 == 0){
                answer += code[i];
            }
            else if (mode == 1 && i%2 == 1){
                answer += code[i];
            }
        }
    }
    if (answer.length() == 0) answer = "EMPTY";
    return answer;
}
728x90
반응형