Life Engineering
[프로그래머스] 이진 변환 반복하기 (C++) 본문
https://programmers.co.kr/learn/courses/30/lessons/70129
#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;
}
'Problem Solving' 카테고리의 다른 글
[프로그래머스] 캐시 (C++) (0) | 2021.11.27 |
---|---|
[프로그래머스] 주식 가격 (C++) (0) | 2021.11.19 |
[BOJ 1038] 감소하는 수 (C++) (0) | 2021.11.11 |
[프로그래머스] 예상 대진표 (C++) (0) | 2021.11.11 |
[프로그래머스] N개의 최소공배수 (C++) (0) | 2021.11.11 |