어렵진 않았음.
10진법을 3진법으로 바꾸는 문제이다. 다만 3으로 나누어 떨어질때, 별도의 처리가 필요했다.
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
string solution(int n) {
string answer = "";
for (int i = 3; n != 0; n /= 3)
{
if (n % 3 == 1)
answer += '1';
else if (n % 3 == 2)
answer += '2';
else
{
answer += '4';
n -= 3;
}
}
reverse(answer.begin(), answer.end());
return answer;
}'Coding_Test 연습 > Programmers' 카테고리의 다른 글
| [프로그래머스] (C++) LV2 더 맵게 (0) | 2022.05.16 |
|---|---|
| [프로그래머스] (C++) LV2 기능개발 (0) | 2022.04.28 |
| [프로그래머스] (C++) LV2 멀쩡한 사각형 (0) | 2022.04.28 |
| [프로그래머스] (C++) LV2 오픈채팅방 (0) | 2022.04.28 |
| [프로그래머스] (C++) LV2 문자열 압축 (0) | 2022.04.28 |