[프로그래머스] 이진 변환 반복하기

 

https://school.programmers.co.kr/learn/courses/30/lessons/70129

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr


 

해결전략

 

문자열 to_string

이진법 변환

 

 


 

코드

 

#include <string>
#include <vector>
using namespace std;

vector<int> solution(string s) {
    vector<int> answer;
    
    int Count = 0, zeroCnt = 0, oneCnt = 0;
    
    while(s.size() > 1)
    {
        for (const auto& i : s) {
            if (i == '1')
                oneCnt++;
            else if (i == '0')
                zeroCnt++;
        }
        
        s.clear();//s 비워주기
        while(oneCnt)//이진수로 전환해서 s에 넣기
        {
            s = to_string(oneCnt % 2) + s;

            oneCnt /= 2;
        }
        
        oneCnt = 0;
        Count++;
    }
    
    answer.push_back(Count);
    answer.push_back(zeroCnt);
    
    return answer;
}