https://programmers.co.kr/learn/courses/30/lessons/12899#
코딩테스트 연습 - 124 나라의 숫자
programmers.co.kr
#include <string>
#include <vector>
#include <iostream>
using namespace std;
string solution(int n) {
string answer = "";
while (n > 3) {
if (n % 3 == 0) {
answer = "4" + answer;
n -= 1;
}
else {
answer = to_string(n % 3) + answer;
}
n /= 3;
}
if (n == 3) {
answer = "4" + answer;
}
else {
answer = to_string(n) + answer;
}
return answer;
}
3진수를 계산하듯 하면서 0일 경우 4로 치환해주고 n -1 을 해준다.
'전공공부 > 코딩테스트' 카테고리의 다른 글
(c++) 프로그래머스 "짝지어 제거하기" (0) | 2022.01.13 |
---|---|
(c++) 프로그래머스 "소수 만들기" (0) | 2022.01.13 |
(c++) 프로그래머스 멀쩡한 사각형 (0) | 2022.01.12 |
(c++) 프로그래머스 단체사진 찍기 (0) | 2022.01.11 |
(c++) 프로그래머스 "카카오 프렌즈 컬러링북" (0) | 2022.01.11 |