From e4d61eb884970b6a76b4b9e229ec2e782f2d0aaf Mon Sep 17 00:00:00 2001 From: MoiJiyeon <118200927+MoiJiyeon@users.noreply.github.com> Date: Fri, 30 Aug 2024 22:21:17 +0900 Subject: [PATCH 01/11] #38 1149.py --- luna/class4-1/1149.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 luna/class4-1/1149.py diff --git a/luna/class4-1/1149.py b/luna/class4-1/1149.py new file mode 100644 index 0000000..2103c94 --- /dev/null +++ b/luna/class4-1/1149.py @@ -0,0 +1,13 @@ +num_house, *costs = map(int,open(0).read().split()) # num_house = 3, costs = [26, 40, 83, 49, 60, 57, 13, 89, 99] + +for i in range(3, len(costs)): # cost의 인덱스는 0부터 시작 + if i == 3: + costs[i] += min(costs[1], costs[2]) + elif i % 3 == 0: + costs[i] += min(costs[i-1], costs[i-2]) + elif i % 3 == 1: + costs[i] += min(costs[i-2], costs[i-4]) + else: + costs[i] += min(costs[i-4], costs[i-5]) + +print(min(costs[-1],costs[-2],costs[-3])) \ No newline at end of file From 5f8ae2fef9cbf7825a12fd4ad1b600101268f463 Mon Sep 17 00:00:00 2001 From: MoiJiyeon <118200927+MoiJiyeon@users.noreply.github.com> Date: Fri, 30 Aug 2024 22:21:29 +0900 Subject: [PATCH 02/11] #38 1629.py --- luna/class4-1/1629.py | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 luna/class4-1/1629.py diff --git a/luna/class4-1/1629.py b/luna/class4-1/1629.py new file mode 100644 index 0000000..9e7bec3 --- /dev/null +++ b/luna/class4-1/1629.py @@ -0,0 +1,6 @@ +A, B, C = map(int,open(0).read().split()) + +result = 1 # A**0 = 항상 1 +for _ in range(B): + result = result * A % C +print(result) \ No newline at end of file From ed32a05852264220ccc77ee2e37632b9f80196bb Mon Sep 17 00:00:00 2001 From: MoiJiyeon <118200927+MoiJiyeon@users.noreply.github.com> Date: Fri, 30 Aug 2024 22:21:37 +0900 Subject: [PATCH 03/11] #38 1932.py --- luna/class4-1/1932.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 luna/class4-1/1932.py diff --git a/luna/class4-1/1932.py b/luna/class4-1/1932.py new file mode 100644 index 0000000..4da1ac8 --- /dev/null +++ b/luna/class4-1/1932.py @@ -0,0 +1,22 @@ +import sys + +def max_sum_triangle(triangle, n): + for i in range(n-2, -1, -1): # 아래에서 위로 역순으로 이동, i의 범위는 n-1 ~ 0 + for j in range(i+1): # j의 범위는 i의 x 좌표의 +0 ~ +1 + triangle[i][j] += max(triangle[i+1][j], triangle[i+1][j+1]) # 아래 두 값 중 더 큰 값을 더함 + + return triangle[0][0] # 최종적으로 가장 위에 최대 합이 남음 + +# 입력 처리 +input = sys.stdin.read +data = input().strip().splitlines() + +# 첫째 줄: 삼각형의 크기 +n = int(data[0]) + +# 다음 n줄: 정수 삼각형의 정보 (각 줄의 값들, 공백 구분) +triangle = [list(map(int, line.split())) for line in data[1:]] + +# 출력 결과 +output = str(max_sum_triangle(triangle, n)) +sys.stdout.write(output + '\n') \ No newline at end of file From 8249d187357117770cd7e9e711900aa161920490 Mon Sep 17 00:00:00 2001 From: MoiJiyeon <118200927+MoiJiyeon@users.noreply.github.com> Date: Fri, 30 Aug 2024 22:21:44 +0900 Subject: [PATCH 04/11] #38 1991.py --- luna/class4-1/1991.py | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 luna/class4-1/1991.py diff --git a/luna/class4-1/1991.py b/luna/class4-1/1991.py new file mode 100644 index 0000000..00d51ab --- /dev/null +++ b/luna/class4-1/1991.py @@ -0,0 +1,32 @@ +N, *lines = open(0).read().splitlines() # 줄마다 읽어옴, n = 7, lines = ['A B C', ...] +data = {a: (b, c) for a, b, c in (line.split() for line in lines)} # {'A': ('B', 'C'), ...} + +def order_n1(i): # 전위 순회 + print(i, end='') # 루트 출력 + left, right = data[i] + if left != '.': + order_n1(left) # 왼쪽 자식 순회 + if right != '.': + order_n1(right) # 오른쪽 자식 순회 + +def order_n2(i): # 중위 순회 + left, right = data[i] + if left != '.': + order_n2(left) # 왼쪽 자식 순회 + print(i, end='') # 루트 출력 + if right != '.': + order_n2(right) # 오른쪽 자식 순회 + +def order_n3(i): # 후위 순회 + left, right = data[i] + if left != '.': + order_n3(left) # 왼쪽 자식 순회 + if right != '.': + order_n3(right) # 오른쪽 자식 순회 + print(i, end='') # 루트 출력 + +order_n1('A') +print() +order_n2('A') +print() +order_n3('A') \ No newline at end of file From da75771aeb78a5b0c93a3245957e8596e30670ae Mon Sep 17 00:00:00 2001 From: MoiJiyeon <118200927+MoiJiyeon@users.noreply.github.com> Date: Fri, 30 Aug 2024 22:21:52 +0900 Subject: [PATCH 05/11] #38 9465.py --- luna/class4-1/9465.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 luna/class4-1/9465.py diff --git a/luna/class4-1/9465.py b/luna/class4-1/9465.py new file mode 100644 index 0000000..07c17ed --- /dev/null +++ b/luna/class4-1/9465.py @@ -0,0 +1,17 @@ +N, *data = [*open(0)] +lines = [list(map(int, _.split())) for _ in data] + +for i in range(int(N)): + num = lines[3*i][0] + top_row = lines[3*i + 1] + bottom_row = lines[3*i + 2] + + if num > 1: + top_row[1] += bottom_row[0] + bottom_row[1] += top_row[0] + + for j in range(2, num): + top_row[j] += max(bottom_row[j-1], bottom_row[j-2]) + bottom_row[j] += max(top_row[j-1], top_row[j-2]) + + print(max(top_row[-1], bottom_row[-1])) \ No newline at end of file From 0b86cc72a149383b6f50e82736a911c5395549d7 Mon Sep 17 00:00:00 2001 From: MoiJiyeon <118200927+MoiJiyeon@users.noreply.github.com> Date: Fri, 30 Aug 2024 22:22:05 +0900 Subject: [PATCH 06/11] #38 11053.py --- luna/class4-1/11053.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 luna/class4-1/11053.py diff --git a/luna/class4-1/11053.py b/luna/class4-1/11053.py new file mode 100644 index 0000000..12d02ca --- /dev/null +++ b/luna/class4-1/11053.py @@ -0,0 +1,25 @@ +import sys + +def largest_A(n, A_s): + dp = [1] * n + + for i in range(1, n): + for j in range(i): + if A_s[j] < A_s[i]: + dp[i] = max(dp[i], dp[j] + 1) + + return max(dp) + +# 입력 처리 +input = sys.stdin.read +data = input().strip().splitlines() + +# 첫째 줄: 수열의 크기 +n = int(data[0]) + +# 둘째 줄: 수열을 이루고 있는 N개의 A_i들 +A_s = list(map(int, data[1].split())) + +# 출력 결과 +output = str(largest_A(n, A_s)) +sys.stdout.write(output + '\n') \ No newline at end of file From 6611d2356d9035cf23311281054d672557d09708 Mon Sep 17 00:00:00 2001 From: MoiJiyeon <118200927+MoiJiyeon@users.noreply.github.com> Date: Fri, 30 Aug 2024 22:22:14 +0900 Subject: [PATCH 07/11] #38 11725.py --- luna/class4-1/11725.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 luna/class4-1/11725.py diff --git a/luna/class4-1/11725.py b/luna/class4-1/11725.py new file mode 100644 index 0000000..63b5285 --- /dev/null +++ b/luna/class4-1/11725.py @@ -0,0 +1,20 @@ +from collections import deque + +N, *edges = map(int, open(0).read().split()) # edges = [1, 6, 6, 3, ...] +tree = [[] for _ in range(N + 1)] + +for a, b in zip(edges[::2], edges[1::2]): # 2씩 건너뛰며 zip [1, 6], [6, 3], ... + tree[a] += [b] # 연결된 노드 리스트 저장 + tree[b] += [a] + +parents = [0] * (N + 1) +queue = deque([1]) + +while queue: + current = queue.popleft() + for next_node in tree[current]: + if not parents[next_node]: + parents[next_node] = current + queue.append(next_node) + +print('\n'.join(map(str, parents[2:]))) # 2번 노드부터 출력 \ No newline at end of file From bbfff20c391ebbce3cf00eb3440d53c47db4bf8a Mon Sep 17 00:00:00 2001 From: MoiJiyeon <118200927+MoiJiyeon@users.noreply.github.com> Date: Fri, 30 Aug 2024 22:22:22 +0900 Subject: [PATCH 08/11] #38 15650py --- luna/class4-1/15650.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 luna/class4-1/15650.py diff --git a/luna/class4-1/15650.py b/luna/class4-1/15650.py new file mode 100644 index 0000000..fcfcd03 --- /dev/null +++ b/luna/class4-1/15650.py @@ -0,0 +1,17 @@ +import sys +from itertools import combinations + +def generate_combinations(n, m): + # 1부터 n까지의 수 중에서 m개를 선택하는 조합(c) 생성 + return combinations(range(1, n+1), m) + +# 입력 처리 +input = sys.stdin.read +data = input().strip().splitlines() + +# 첫째 줄: 자연수 N, M +N, M = map(int, data[0].split()) + +# 출력 결과 +output = '\n'.join(' '.join(map(str, combo)) for combo in generate_combinations(N, M)) +sys.stdout.write(output + '\n') \ No newline at end of file From 7fe45e8c76ccf6d7509a3a541e16622ecc5a36b9 Mon Sep 17 00:00:00 2001 From: MoiJiyeon <118200927+MoiJiyeon@users.noreply.github.com> Date: Fri, 30 Aug 2024 22:22:31 +0900 Subject: [PATCH 09/11] #38 15654.py --- luna/class4-1/15654.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 luna/class4-1/15654.py diff --git a/luna/class4-1/15654.py b/luna/class4-1/15654.py new file mode 100644 index 0000000..85c4c8d --- /dev/null +++ b/luna/class4-1/15654.py @@ -0,0 +1,19 @@ +import sys +from itertools import permutations + +def generate_permutations(numbers, m): + # n개의 수(=numbers) 중에서 m개를 선택하는 순열(p) 생성 + return sorted(permutations(numbers, m)) + +# 입력 처리 +input = sys.stdin.read +data = input().strip().splitlines() + +# 첫째 줄: 자연수 N, M (1 <= M <= N <= 8) +N, M = map(int, data[0].split()) +# 둘째 줄: N개의 수 (x <= 10,000) +nums = list(map(int, data[1].split())) + +# 출력 결과 +output = '\n'.join(' '.join(map(str, perm)) for perm in generate_permutations(nums, M)) +sys.stdout.write(output + '\n') \ No newline at end of file From 2fd0b37299788955255870d844d180e54a92c590 Mon Sep 17 00:00:00 2001 From: MoiJiyeon <118200927+MoiJiyeon@users.noreply.github.com> Date: Fri, 30 Aug 2024 22:24:27 +0900 Subject: [PATCH 10/11] #38 15663.py --- luna/class4-1/15663.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 luna/class4-1/15663.py diff --git a/luna/class4-1/15663.py b/luna/class4-1/15663.py new file mode 100644 index 0000000..fbb3b6d --- /dev/null +++ b/luna/class4-1/15663.py @@ -0,0 +1,13 @@ +from itertools import permutations, starmap + +# open(0) : 표준 입력 받아오기 +NM, nums = [*open(0)] # NM = "3 1\n", nums = "4 4 2\n" + +# nums의 개행문자를 지우고 int로 변환해서 set에 넣은 후 정렬 +sorted_set = sorted({*permutations(map(int, nums.split()), int(NM[2]))}) # {(2, 4, 4), (4, 2, 4), (4, 4, 2)} + +for perm in sorted_set: + print(*perm) + +# 아래처럼 starmap 사용 가능 +# *starmap(print,sorted({*permutations(map(int, nums.split()), int(NM[2]))})), \ No newline at end of file From cd82a060f25552e4f088397506223688baf3cbff Mon Sep 17 00:00:00 2001 From: MoiJiyeon <118200927+MoiJiyeon@users.noreply.github.com> Date: Fri, 30 Aug 2024 22:38:16 +0900 Subject: [PATCH 11/11] =?UTF-8?q?#38=201629.py=20=ED=8C=8C=EC=9D=BC=20?= =?UTF-8?q?=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- luna/class4-1/1629.py | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/luna/class4-1/1629.py b/luna/class4-1/1629.py index 9e7bec3..421b220 100644 --- a/luna/class4-1/1629.py +++ b/luna/class4-1/1629.py @@ -1,6 +1,2 @@ -A, B, C = map(int,open(0).read().split()) - -result = 1 # A**0 = 항상 1 -for _ in range(B): - result = result * A % C -print(result) \ No newline at end of file +A, B, C = map(int, input().split()) +print(pow(A, B, C)) \ No newline at end of file