๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿš“ Self Study/๐Ÿ”“ Programmers

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค (124 ๋‚˜๋ผ, ์—ฐ์Šต๋ฌธ์ œ) C++

by UKHYUN22 2022. 1. 3.
728x90

 

์žฌ๊ท€ํ•จ์ˆ˜๋กœ ํ–ˆ์—ˆ๋‹ค๊ฐ€ ์ฝ”๋“œ ๋‚ญ๋น„๊ฐ€ ๋งŽ์•„์ ธ์„œ ๋ฌดํ•œ๋ฃจํ”„ ์•ˆ์—์„œ ํ•ด๊ฒฐํ–ˆ๋‹ค.

 

#include <string>
#include <vector>
#include <iostream>

using namespace std;


string solution(int n) {
    string answer = "";
    int top = n;
    int bottom = 0;
    
    while(true) {
        bottom = top % 3;
        top = top / 3;
        
        switch(bottom) {
            case 0:
                answer = '4' + answer;
                top -= 1;
                break;
            case 1:
                answer = '1' + answer;
                break;
            case 2:
                answer = '2' + answer;
                break;
        }
        if(top == 0)
            break;
    }
    
    return answer;
}