C++)
#include <string>
#include <vector>
#include <stack>
using namespace std;
int solution(int n) {
// n(3진법)
stack<int> s;
while(n > 0) {
s.push(n%3);
n/=3;
}
// 10진법으로 표현
int answer = 0;
int value = 1;
while(!s.empty()) {
answer += (s.top() * value);
s.pop();
value *= 3;
}
return answer;
}
Python)
def func(number):
ans = ''
while number > 0:
ans += str(number % 3)
number //= 3
return int(ans)
def solution(n):
num = func(n)
ans = num%10
cur = 1
num //= 10
while num > 0:
ans += (num%10) * (3**cur)
num //= 10
cur += 1
return ans
이렇게 풀었었는데 파이썬에는 놀라운 기능이 있었다.
파이썬 진수변환(2진법, 3진법, 5진법, 10진법)[n진법]
python에서는 기본적으로 int() 라는 함수를 지원한다int(string, base)2051104185276710진수로 변경이 가능하다.2, 8, 16진수는 bin(), oct(), hex() 함수를 지원한다.0b10110o130xb0b는 2진수, 0o는 8진수,
velog.io
def func(number):
ans = ''
while number > 0:
ans += str(number % 3)
number //= 3
return ans
def solution(n):
num = func(n)
return int(num, 3)'프로그래머스 > Level1' 카테고리의 다른 글
| [Level1] 달리기 경주 (C++) (0) | 2023.04.26 |
|---|---|
| [Level1] 삼총사 (C++/Python) (0) | 2023.04.25 |
| [Level1] 대충 만든 자판 (C++) (0) | 2023.02.27 |
| [Level1] 성격 유형 검사하기 (C++) (0) | 2023.02.26 |
| [Level1] 카드 뭉치 (C++) (0) | 2023.02.25 |