본문 바로가기
반응형

{ Problem Solving }/Recursion7

[재귀 / 파이썬] 백준 1030번 : 프렉탈 평면*** / 골드 3 https://www.acmicpc.net/problem/1030 1030번: 프렉탈 평면 첫째 줄에 7개의 정수 s, N, K, R1, R2, C1, C2가 주어진다. www.acmicpc.net s, n, k, x1, x2, y1, y2 = map(int, input().split()) m = (n - k) // 2 def check(x, y, t): if t == 0: return 0 nn = n**(t-1) nx = x // nn ny = y // nn if m 2022. 3. 8.
[재귀 / 파이썬] 백준 10993번 : 별 찍기 -18 / 골드 4 https://www.acmicpc.net/problem/10993 10993번: 별 찍기 - 18 예제를 보고 규칙을 유추한 뒤에 별을 찍어 보세요. www.acmicpc.net n = int(input()) def star(depth): if depth == 1: return ['*'] m = 2**(depth+1)-3 table = [[' ' for _ in range(m)] for _ in range(m)] k = 2**depth-3 shift = 2**(depth-1) if depth % 2 == 0: for i in range(m): table[0][i] = '*' j = 0 for i in range(0, m, 2): table[i][j] = '*' j += 1 j -= 1 for i in .. 2022. 3. 7.
[재귀 / 파이썬 ] 백준 1662번 : 압축 / 골드 5 https://www.acmicpc.net/problem/1662 1662번: 압축 압축되지 않은 문자열 S가 주어졌을 때, 이 문자열중 어떤 부분 문자열은 K(Q)와 같이 압축 할 수 있다. K는 한자리 정수이고, Q는 0자리 이상의 문자열이다. 이 Q라는 문자열이 K번 반복된다는 뜻이 www.acmicpc.net import collections s = collections.deque([x for x in input()]) def recursion(s): if len(s) == 1: return 1 temp = 0 while s: cur = s.popleft() if not s: temp += 1 break if cur.isnumeric() and s[0] != '(': temp += 1 elif c.. 2022. 3. 7.
[재귀/파이썬] 백준 4256번: 트리 / 골드 3 https://www.acmicpc.net/problem/4256 4256번: 트리 첫째 줄에 테스트 케이스의 개수 T가 주어진다. 각 테스트 케이스의 첫째 줄에는 노드의 개수 n이 주어진다. (1 ≤ n ≤ 1,000) BT의 모든 노드에는 1부터 n까지 서로 다른 번호가 매겨져 있다. 다음 www.acmicpc.net import sys input = sys.stdin.readline class BT(): def __init__(self, val=0, left=None, right=None): self.val = val self.left = left self.right = right def recursion(preorder, inorder): if not preorder: return None bt .. 2022. 3. 7.
[Recursion/파이썬] 백준 11729번: 하노이 탑 이동 순서 / 실버 1 https://www.acmicpc.net/problem/11729 11729번: 하노이 탑 이동 순서 세 개의 장대가 있고 첫 번째 장대에는 반경이 서로 다른 n개의 원판이 쌓여 있다. 각 원판은 반경이 큰 순서대로 쌓여있다. 이제 수도승들이 다음 규칙에 따라 첫 번째 장대에서 세 번째 장대로 www.acmicpc.net n = int(input()) def recursion(num, cur, next): if num == 1: print(cur, next) return recursion(num-1, cur, 6-cur-next) print(cur, next) recursion(num-1, 6-cur-next, next) print(pow(2, n)-1) recursion(n, 1, 3) 지금 이 문제.. 2022. 3. 6.
[재귀] 백준 2448번 : 별 찍기 - 11 / 골드 4 https://www.acmicpc.net/problem/2448 2448번: 별 찍기 - 11 첫째 줄에 N이 주어진다. N은 항상 3×2k 수이다. (3, 6, 12, 24, 48, ...) (0 ≤ k ≤ 10, k는 정수) www.acmicpc.net n = int(input()) def star(k): if k == 3: return [[' ', ' ', '*', ' ', ' '], [' ', '*', ' ', '*', ' '], ['*', '*', '*', '*', '*']] output = [] unit = star(k // 2) k = k//2 for i in range(2*k): if i < k: output.append([' ']*k + unit[i] + [' ']*k) else: ou.. 2021. 11. 16.
반응형