Notice
Recent Posts
Recent Comments
Link
«   2024/09   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30
Archives
Today
Total
관리 메뉴

Life Engineering

[프로그래머스] 이진 변환 반복하기 (C++) 본문

Problem Solving

[프로그래머스] 이진 변환 반복하기 (C++)

흑개 2021. 11. 18. 16:52

https://programmers.co.kr/learn/courses/30/lessons/70129

 

코딩테스트 연습 - 이진 변환 반복하기

 

programmers.co.kr

#include <string>
#include <vector>
#include <algorithm>

using namespace std;

int zero=0;
int cnt=0;

int stepOne(string s){
    int c=0;
    for (char i : s){
        if (i=='0')
            zero++;
        else
            c++;
    }
    return c;
}

string stepTwo(int c){
    string temp="";
    while (true){
        temp+=to_string(c%2);
        c=c/2;
        if (c==0)
            break;
    }
    reverse(temp.begin(), temp.end());
    return temp;
}

vector<int> solution(string s) {
    vector<int> answer;
    while (s!="1"){
        int c=stepOne(s);
        s=stepTwo(c);
        cnt++;
    }
    answer.push_back(cnt);
    answer.push_back(zero);
    return answer;
}