https://school.programmers.co.kr/learn/courses/30/lessons/70129
아 왜이리 헷갈리는지 모르겠다. 근데 푸니까 1점 올라가네 기준이 뭐냐 도당체
#include <string>
#include <vector>
#include <iostream>
using namespace std;
string binary_transformation(int num)
{
string res = "";
int multi = 1;
while (2 * multi <= num)
multi *= 2;
// cout << num << ' ' << multi << '\n';
while (num > 0 && multi >= 1)
{
if (num - multi >= 0)
{
res += '1';
num -= multi;
}
else
res += '0';
multi /= 2;
}
while (multi > 0)
{
res += '0';
multi /= 2;
}
return res;
}
vector<int> solution(string s) {
vector<int> answer;
int change_cnt = 0, zero_cnt = 0;
while (s != "1")
{
int cur_one_cnt = 0;
for (int i = 0; i < s.size(); i++)
if (s[i] == '1')
cur_one_cnt++; // 0의 개수
zero_cnt += s.size() - cur_one_cnt;
s = binary_transformation(cur_one_cnt);
// cout << s << '\n';
change_cnt++;
}
answer.push_back(change_cnt);
answer.push_back(zero_cnt);
return answer;
}
'<algorithm> > 프로그래머스' 카테고리의 다른 글
프로그래머스 다음 큰 숫자 c++ (0) | 2023.05.25 |
---|---|
프로그래머스 숫자의 표현 c++ (0) | 2023.05.25 |
프로그래머스 최댓값과 최솟값 c++ (0) | 2023.05.25 |
프로그래머스 jadencase 문자열 만들기 c++ (0) | 2023.05.25 |
프로그래머스 신규 아이디 추천 c++ (2) | 2023.05.24 |