코딩테스트/프로그래머스

[프로그래머스] 124 나라의 숫자

쪼르뚜 2024. 9. 5. 14:53
728x90
반응형


🔗 문제 링크

https://school.programmers.co.kr/learn/courses/30/lessons/12899

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr


반응형
728x90

👩‍💻 코드

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

using namespace std;

string solution(int n) {
    string answer = "";
    string expression = "412";
    
    while(n > 0){
        int remainder = n % 3;
    
        answer = expression[remainder] + answer;
        
        n /= 3;
        
        if(remainder == 0){
            n--;
        }
    }
    
    
    return answer;
}

📝 풀이

이 문제는 10진법을 3진법으로 변환한다고 생각하면 쉽다.

똑같은 방식으로 변환하되 주의 할 점은 나머지가 0인 경우 몫에서 1을 빼주어야 한다.

728x90
반응형