그냥 막히는거 없이 구현했다.
별도의 알고리즘도 필요하지 않아 쉬운거같다.
#include <iostream>
#include <string>
#include <vector>
using namespace std;
string TO_BINARY(int n)
{
string re = "";
for (; n != 0; n /= 2)
{
if (n % 2 == 0)
re = '0' + re;
else
re = '1' + re;
}
return re;
}
vector<int> solution(string s) {
vector<int> answer(2, 0);
while (1)
{
int cnt = 0;
if (s == "1")
break;
for (int i = 0; i < s.size(); i++)
if(s[i] == '1')
cnt++;
answer[0]++;
answer[1] += s.size() - cnt;
s = TO_BINARY(cnt);
}
return answer;
}'Coding_Test 연습 > Programmers' 카테고리의 다른 글
| [프로그래머스] (C++) LV2 3 x n 타일링 (0) | 2022.06.04 |
|---|---|
| [프로그래머스] (C++) LV2 점프와 순간 이동 (0) | 2022.06.04 |
| [프로그래머스] (C++) LV2 모음 사전 (0) | 2022.06.03 |
| [프로그래머스] (C++) LV2 전력망을 둘로 나누기 (0) | 2022.06.02 |
| [프로그래머스] (C++) LV2 구명보트 (0) | 2022.06.01 |