1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | #include <string> #include <vector> #include <iostream> #include <algorithm> using namespace std; string result = ""; vector<string> offset = {"0", "1", "2", "4"}; void notation(int value) { int quotient = value / 3; int remainder = value % 3; if (remainder == 0) { quotient = ((value - 1) / 3); remainder = ((value - 1) % 3) + 1; } if (quotient <= 3) { result += offset[remainder]; if (quotient != 0) result += offset[quotient]; } else { result += offset[remainder]; notation(quotient); } } string solution(int n) { notation(n); reverse(result.begin(), result.end()); return result; } | cs |
주어진 값 n을 1, 2, 4만 사용하는 3진수로 치환하여 출력하는 문제이다.
((3보다 작은 몫) + (n 회의 나머지)) 를 (1,2,4)진법에 맞추어서 변환하면 값을 구할 수 있다.
성의없이 짠 코드라 다듬을 필요가 있지만 문제에서 요구하는 Time complexity는 충족하는 소스코드이다.




덧글