[프로그래머스] 이진 변환 반복하기
https://school.programmers.co.kr/learn/courses/30/lessons/70129
해결전략
문자열 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;
}