728x90
옛날 옛적엔 못풀었던거같은데, 이번엔 30분도 안걸린거같다 :)
더보기
#include <string>
#include <vector>
using namespace std;
vector<string> one_two_four = {"", "1", "2", "4"};
string solution(int n) {
string answer = "";
int quotient = n, remainder = n%3;
do {
remainder = quotient%3;
quotient = quotient/3;
if(remainder == 0) {
quotient -= 1;
remainder = 3;
}
answer = one_two_four[remainder] + answer;
if(quotient <= 3) answer = one_two_four[quotient] + answer;
} while(quotient > 3);
return answer;
}
문제
https://programmers.co.kr/learn/courses/30/lessons/12899
코딩테스트 연습 - 124 나라의 숫자
programmers.co.kr
728x90
[접근]
손으로 직접 풀어보니 3으로 난웠을 때
나머지가 각각 0이면 4, 1이면 1, 2이면 2를 나타냈는데 이때 0일때는 몫에 1을 빼준다.
이 숫자는 answer의 왼쪽에 추가됐다.
나머지를 통해 구해야할 것을 구하고 난 뒤 계산된 몫이 4이상이면 계속 나누기를 반복했고
3이하면 1이면 1, 2이면 2, 3이면 4를 마지막으로 추가해준 뒤 do-while문을 빠져나왔다.
int quotient = n, remainder = n%3;
do {
remainder = quotient%3;
quotient = quotient/3;
if(remainder == 0) {
quotient -= 1;
remainder = 3;
}
answer = one_two_four[remainder] + answer;
if(quotient <= 3) answer = one_two_four[quotient] + answer;
} while(quotient > 3);728x90
'coding > algorithm' 카테고리의 다른 글
| [백준/baekjoon] Silver4 1388. 바닥 장식 (0) | 2022.05.22 |
|---|---|
| [SW Expert Academy] D1 2071. 평균값 구하기(c++) (0) | 2022.05.20 |
| [programmers] Lv.1 없는 숫자 더하기(c++) (0) | 2022.05.19 |
| [SW Expert Academy] D2 1204. 최빈수 구하기(c++) (0) | 2022.05.19 |
| [programmers] Lv.2 멀쩡한 사각형(c++) - 2nd (0) | 2022.05.18 |
댓글