알고리즘
[알고리즘] 이진 변환 반복하기
keel_im
2020. 11. 6. 10:56
반응형
포인트
1. 구현 문제와 진법 변환을 같이 한거라고 할 수 있다. 진법 변환은 생각보다 많이 쓸 수 있다.
2. string 을 사용하는 이유는 int ~ long long 범위를 만족하기 위해서 입니다.
🧶문서는 항상 수정 될 수 있습니다. 비판은 환영합니다.
c++/cpp
#include <bits/stdc++.h>
using namespace std;
string n_to_2(int one){
string bin;
while(one>0){
bin = to_string(one%2)+bin;
one/=2;
}
return bin;
}
vector<int> solution(string s) {
vector<int> answer;
int i=0;
int zero = 0;
while(true){
i+=1;
zero+=count(s.begin(), s.end(), '0');
int one = count(s.begin(), s.end(), '1');
s = n_to_2(one);
if(s=="1") break;
}
answer.push_back(i);
answer.push_back(zero);
return answer;
}
python
from typing import Counter
def solution(s):
answer = []
zero = 0
i = 0
while True:
i+=1
zero+=s.count('0')
s= bin(s.count('1')).replace('0b','')
if(s=='1'):
break
return [i, zero]
반응형