[Programmers] Lv2. ์ด์ง ๋ณํ ๋ฐ๋ณตํ๊ธฐ | C++
2023. 6. 12. 20:45ใCoding Test/Programmers
๐๋ฌธ์ ๋ณด๋ฌ๊ฐ๊ธฐ
๐จ๐ปํ์ด ๊ณผ์
์ฌ์ค์ ํ์ด ๊ณผ์ ์ด๋ผ ํ ๊ฒ๋ ์์ต๋๋ค. ๋ฌธ์ ์์ ์ ์ํ ๋ด์ฉ ๊ทธ๋๋ก ์ฝ๋๋ก ๊ตฌํํ๋ฉด ๋ฉ๋๋ค.
โ๏ธ์์ค ์ฝ๋ ๋ฐ ๊ฒฐ๊ณผ
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
string RemoveZero(const string& str, int& removedZeroCount) {
string result;
for (const auto element : str) {
if (element == '1') {
result.push_back('1');
continue;
}
removedZeroCount++;
}
return result;
}
string ConvertBinary(const string& oneBinary) {
string result;
int targetNum = oneBinary.length();
while (true) {
int remainder = targetNum % 2;
targetNum /= 2;
char numericChar = remainder + '0';
result.push_back(numericChar);
if (targetNum == 0)
break;
}
reverse(result.begin(), result.end());
return result;
}
bool IsCanBinaryConverting(const string& str) {
return str.length() > 1;
}
vector<int> solution(string s) {
int removedZeroCount = 0;
int binaryConvertingCount = 0;
string temp = s;
while (true) {
binaryConvertingCount++;
string removedZero = RemoveZero(temp, removedZeroCount);
if (IsCanBinaryConverting(removedZero)) {
temp = ConvertBinary(removedZero);
continue;
}
break;
}
vector<int> answer {binaryConvertingCount, removedZeroCount};
return answer;
}
728x90
๋ฐ์ํ
'Coding Test > Programmers' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Programmers] Lv2. ์ง์ง์ด ์ ๊ฑฐํ๊ธฐ | C++ (0) | 2023.06.12 |
---|---|
[Programmers] Lv2. ํผ๋ณด๋์น ์ | C++ (0) | 2023.06.12 |
[Programmers] Lv2. ์ซ์์ ํํ | C++ (0) | 2023.06.12 |
[Programmers] Lv2. ์ต์๊ฐ ๋ง๋ค๊ธฐ | C++ (1) | 2023.06.11 |
[Programmers] Lv2. JadenCase ๋ฌธ์์ด ๋ง๋ค๊ธฐ | C++ (0) | 2023.06.11 |