반응형
https://www.acmicpc.net/problem/4779
4779번: 칸토어 집합
칸토어 집합은 0과 1사이의 실수로 이루어진 집합으로, 구간 [0, 1]에서 시작해서 각 구간을 3등분하여 가운데 구간을 반복적으로 제외하는 방식으로 만든다. 전체 집합이 유한이라고 가정하고,
www.acmicpc.net
import sys
input = sys.stdin.readline
def cantorian(num):
if num == 1:
return '-'
l = num//3
str = cantorian(l)
return str + ' '*l + str
while True:
try:
n = int(input())
num = 3**n
print(cantorian(num))
except:
break
재귀적으로 풀이한 방법.
num이 1이 될 때 탈출 조건으로 주고 '-'를 리턴시킨 후 그 문자열을 받아 그 다음 문자열 길이(3)를 조합해서 리턴한다.
재귀는 항상 어려운데
Python Tutor: Learn Python, JavaScript, C, C++, and Java programming by visualizing code
Learn Python, JavaScript, C, C++, and Java This tool helps you learn Python, JavaScript, C, C++, and Java programming by visualizing code execution. You can use it to debug your homework assignments and as a supplement to online coding tutorials. Over 15 m
pythontutor.com
위의 사이트를 이용해서 파이썬이 실행될 때의 과정을 시각화해서 볼 수 있어 이해에 도움이 된다.
반응형
'Algorithm' 카테고리의 다른 글
[백준][2343] python - 기타레슨문제로 알아보는 이분탐색과 파라미터 탐색 (1) | 2023.12.27 |
---|---|
[프로그래머스][DFS/BFS] python - 타겟넘버 (0) | 2023.08.21 |
[백준][7576][python] 토마토 - BFS (0) | 2023.08.09 |
[BOJ][python] 피보나치 함수 - 1003 동적 계획법 1 (0) | 2022.03.30 |
[정렬] 안정정렬(stable)과 불안정정렬(not stable) (0) | 2022.03.25 |