Publish:

태그: ,

카테고리:

https://school.programmers.co.kr/learn/courses/30/lessons/70129
난이도 ⭐⭐

문제

이진 변환 반복
이진 변환 반복
이진 변환 반복
이진 변환 반복


나의 풀이

일단 반복문 진입 후… 먼저 이진 변환 횟수 calcCount를 더해준다.
이후 문자열 s를 체크해서 0이 있으면 zeroCount 증가시킴
0이 아니라면 0이 없는 문자열 noZeroString 에 1 넣어줌
0이 없는 문자열 noZeroString의 길이만큼 또 반복문 돌면서 이진수 변환, 변환한 이진수를 다시 s에 넣어줌
다시 while 반복문 반복

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <string>
#include <vector>

using namespace std;

vector<int> solution(string s)
{
    vector<int> answer;

    int calcCount = 0;
    int zeroCount = 0;

    while (s != "1")
    {
        calcCount++;

        string noZeroString = "";

        for (int i = 0; i < s.size(); i++)
        {
            if (s[i] == '0')
            {
                zeroCount++;
            }
            else
            {
                noZeroString += "1";
            }
        }

        int number = noZeroString.size();
        s = "";

        while (number > 0)
        {
            s += to_string(number % 2);
            number /= 2;
        }
    }

    answer.push_back(calcCount);
    answer.push_back(zeroCount);

    return answer;
}

이슈 및 공부한 것을 기록해두는 개인 블로그 입니다. 댓글, 피드백 환영합니다 🙂

Update:

댓글남기기