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;
}
'전공공부 > 코딩테스트' 카테고리의 다른 글
(c++) 프로그래머스 "방문 길이" (0) | 2022.04.15 |
---|---|
(c++) 프로그래머스 "2개 이하로 다른 비트" (0) | 2022.04.14 |
(c++) 프로그래머스 "괄호 회전하기" (0) | 2022.04.13 |
(c++) 프로그래머스 "피로도" (0) | 2022.04.13 |
(c++) 프로그래머스 "[1차] 프렌즈4블록" (미완성) (0) | 2022.04.13 |