전공공부/코딩테스트
(c++) 프로그래머스 "이진 변환 반복하기"
시아나
2022. 4. 13. 15:25
https://programmers.co.kr/learn/courses/30/lessons/70129
코딩테스트 연습 - 이진 변환 반복하기
programmers.co.kr
나의풀이
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
vector<int> solution(string s) {
vector<int> answer = { 0,0 };
while (s != "1") {
//0제거
sort(s.begin(), s.end());
int index = find(s.begin(), s.end(), '1') - s.begin();
answer[1] += index;
s = s.substr(index);
//2진법
int n = s.size();
s = "";
while (n) {
s = to_string(n % 2) + s;
n /= 2;
}
answer[0]++;
}
return answer;
}