[ํ๋ก๊ทธ๋๋จธ์ค-Lv1] 3์ง๋ฒ ๋ค์ง๊ธฐ / Python
๐ Problem Solving/Programmers
import sys
input = sys.stdin.readline
n = int(input())
def solution(n):
temp = []
answer = 0
if n == 1:
return 1
while n > 1:
temp.append(n % 3)
n //= 3
if n == 1:
temp.append(n)
for i in range(len(temp)):
answer += int(temp[i]) * (3 ** (len(temp) - i - 1))
return answer
print(solution(n))
ํด์ค
n์ 3์ผ๋ก ๋๋๋ฉด์ ๋๋จธ์ง๋ฅผ temp์ ์ ์ฅํ๋ฉด ๊ตณ์ด ๋ค์ง์ ํ์๊ฐ ์๋ค.
ex)
45 -> 1200
45%3 = 0, 15%3 = 0, 5%3 = 2, 5//3 = 1 -> 0021
temp์ ๊ฐ๋ค๋ก ๋ค์ 10์ง์๋ก ๋ณํํ๋ฉด ๋!
'๐ Problem Solving > Programmers' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[ํ๋ก๊ทธ๋๋จธ์ค-Lv1] [1์ฐจ] ๋น๋ฐ์ง๋ / Python (0) | 2021.06.02 |
---|---|
[ํ๋ก๊ทธ๋๋จธ์ค-Lv1] ๋ ๊ฐ ๋ฝ์์ ๋ํ๊ธฐ / Python (0) | 2021.06.02 |
[ํ๋ก๊ทธ๋๋จธ์ค-Lv1] ์์ฐ / Python (0) | 2021.06.02 |
[ํ๋ก๊ทธ๋๋จธ์ค-Lv1] ์ฝ์์ ๊ฐ์์ ๋ง์ / Python (0) | 2021.06.01 |
[ํ๋ก๊ทธ๋๋จธ์ค-Lv1] ์คํจ์จ / Python (0) | 2021.06.01 |