슈뢰딩거의 고등어
[프로그래머스] 이진 변환 반복하기 본문
https://programmers.co.kr/learn/courses/30/lessons/70129
[풀이방법]
1. 1의 갯수를 센다
2. 1의 갯수를 2진법으로 바꾼다
1~2 를 반복한다. (값이 한자리 수가 될때까지)
#include <string>
#include <vector>
#include <cmath>
#include <iostream>
using namespace std;
vector<int> solution(string s) {
vector<int> answer;
long long zero_cnt = 0;
long long times = 0;
while(true) {
long long num = 0;
if(s.size() <= 1)
break;
times++;
for(auto w: s) {
if(w == '0') {
zero_cnt++;
}
else
num++;
}
vector <long long> tmp; tmp.clear();
while(num > 0) {
tmp.push_back(num % 2);
num /= 2;
}
long long new_no = 0;
for(long long i=0; i<tmp.size(); i++)
new_no += tmp[i] * pow(10, i);
s = to_string(new_no);
}
answer.push_back(times);
answer.push_back(zero_cnt);
return answer;
}
'알고리즘' 카테고리의 다른 글
[boj] 2751 수 정렬하기 2 (0) | 2022.03.19 |
---|---|
[프로그래머스] 쿼드압축 후 개수 세기 (0) | 2022.03.19 |
[프로그래머스] 짝지어 제거하기 (0) | 2022.03.16 |
[프로그래머스] 카카오 인턴 - 수식 최대화 (0) | 2022.03.16 |
[BOJ] 15829 Hashing (modulo) (0) | 2022.03.15 |
Comments