-
Notifications
You must be signed in to change notification settings - Fork 4
/
Lv2_이진변환반복하기.cpp
50 lines (42 loc) · 859 Bytes
/
Lv2_이진변환반복하기.cpp
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
46
47
48
49
50
#include <string>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
string digit(int n) {
string s = "";
while (n != 0) {
s = s+ to_string(n % 2);
n /= 2;
}
return s;
}
int countZero(string s) {
int ret = 0;
for (auto a : s) {
if (a == '0')
++ret;
}
return ret;
}
vector<int> solution(string s) {
vector<int> answer;
int cnt=0,zcnt = 0;
int round = 0;
do {
zcnt = countZero(s);
cnt += zcnt;
s.erase(remove(s.begin(), s.end(), '0'), s.end());
s = digit(s.length());
++round;
} while (s != "1");
answer.push_back(round);
answer.push_back(cnt);
return answer;
}
int main() {
vector<int> ans = solution("01110");
for (auto a : ans)
cout << a << " ";
return 0;
}