기록하고 까먹지 말기

4779 본문

전공/백준

4779

yha97 2023. 5. 25. 16:22

날짜 : 2023. 05. 25

사용 언어 : python

 

문제

https://www.acmicpc.net/problem/4779

 

 

코드

import sys


def recur(idx, length, flag):
    if length == 0:
        return
    if flag:
        recur(idx, length // 3, True)
        recur(idx + length // 3, length // 3, False)
        recur(idx + (length // 3 * 2), length // 3, True)
    else:
        for i in range(idx, idx + length):
            line[i] = " "
    return


while True:
    try:
        n = int(sys.stdin.readline())
        line = ['-'] * (3 ** n)
        recur(0, len(line), True)
        for i in line:
            print(i, end="")
        print()
    except:
        break

 

 

풀이

- 재귀를 통해 해결한다

- 3의 n승만큼 배열을 만든 후 0*len, 1*len, 2*len으로 재귀

- 공백을 만들기 위한 플래그를 설정 후 1*len일 때에는 길이만큼 여백으로 변경 후 리턴

- 재귀의 끝에 도달한 경우(len = 0) 리턴

 

 

알게된 점

- 이전에 풀었던 문제랑 비슷해서 곧바로 풀 수 있었다.

 

 

참고 사이트

 

'전공 > 백준' 카테고리의 다른 글

17298  (0) 2023.06.01
1940  (0) 2023.05.28
1969  (0) 2023.05.22
2529  (0) 2023.05.21
1759  (0) 2023.05.20