From 54da02e6a81606148727ef061636324157eb1459 Mon Sep 17 00:00:00 2001 From: cheris8 Date: Fri, 11 Sep 2020 20:30:37 +0900 Subject: [PATCH 1/4] =?UTF-8?q?=EA=B9=80=EC=B1=84=ED=98=95=20Section=205?= =?UTF-8?q?=20=EC=97=85=EB=A1=9C=EB=93=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...1_\352\271\200\354\261\204\355\230\225.py" | 31 ++++++++++++++++ ...3_\352\271\200\354\261\204\355\230\225.py" | 37 +++++++++++++++++++ ...6_\352\271\200\354\261\204\355\230\225.py" | 27 ++++++++++++++ ...9_\352\271\200\354\261\204\355\230\225.py" | 30 +++++++++++++++ 4 files changed, 125 insertions(+) create mode 100644 "section_5/Q1/Section5_Q1_\352\271\200\354\261\204\355\230\225.py" create mode 100644 "section_5/Q3/Section5_Q3_\352\271\200\354\261\204\355\230\225.py" create mode 100644 "section_5/Q6/Section5_Q6_\352\271\200\354\261\204\355\230\225.py" create mode 100644 "section_5/Q9/Section5_Q9_\352\271\200\354\261\204\355\230\225.py" diff --git "a/section_5/Q1/Section5_Q1_\352\271\200\354\261\204\355\230\225.py" "b/section_5/Q1/Section5_Q1_\352\271\200\354\261\204\355\230\225.py" new file mode 100644 index 0000000..b8e885b --- /dev/null +++ "b/section_5/Q1/Section5_Q1_\352\271\200\354\261\204\355\230\225.py" @@ -0,0 +1,31 @@ +""" +Section 5 - Q1 + +가장 큰수 + +선생님은 현수에게 숫자 하나를 주고, +해당 숫자의 자릿수들 중 m개의 숫자를 제거하여 가장 큰 수를 만들라고 했습니다. +여러분이 현수를 도와주세요.(단 숫자의 순서는 유지해야 합니다) +만약 5276823 이 주어지고 3개의 자릿수를 제거한다면 +7823이 가장 큰 숫자가 됩니다. +""" + +import sys +sys.stdin = open("input.txt", "rt") + +number, m = map(int, input().split()) +number = list(map(int, str(number))) + +stack = [] +for x in number: + while stack and m>0 and stack[-1] Date: Sat, 12 Sep 2020 16:47:14 +0900 Subject: [PATCH 2/4] =?UTF-8?q?=EA=B9=80=EC=B1=84=ED=98=95=20Section=204?= =?UTF-8?q?=20=EC=97=85=EB=A1=9C=EB=93=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...1_\352\271\200\354\261\204\355\230\225.py" | 40 +++++++++++++++++++ ...2_\352\271\200\354\261\204\355\230\225.py" | 35 ++++++++++++++++ ...3_\352\271\200\354\261\204\355\230\225.py" | 36 +++++++++++++++++ ...7_\352\271\200\354\261\204\355\230\225.py" | 22 ++++++++++ 4 files changed, 133 insertions(+) create mode 100644 "section_4/Q1/Section4_Q1_\352\271\200\354\261\204\355\230\225.py" create mode 100644 "section_4/Q2/Section4_Q2_\352\271\200\354\261\204\355\230\225.py" create mode 100644 "section_4/Q3/Section4_Q3_\352\271\200\354\261\204\355\230\225.py" create mode 100644 "section_4/Q7/Section4_Q7_\352\271\200\354\261\204\355\230\225.py" diff --git "a/section_4/Q1/Section4_Q1_\352\271\200\354\261\204\355\230\225.py" "b/section_4/Q1/Section4_Q1_\352\271\200\354\261\204\355\230\225.py" new file mode 100644 index 0000000..918a5f1 --- /dev/null +++ "b/section_4/Q1/Section4_Q1_\352\271\200\354\261\204\355\230\225.py" @@ -0,0 +1,40 @@ +""" +Section 4 - Q1 + +이분검색 + +임의의 N개의 숫자가 입력으로 주어집니다. +N개의 수를 오름차순으로 정렬한 다음 N개의 수 중 한 개의 수인 M이 주어지면 +이분검색으로 M이 정렬된 상태에서 몇 번째에 있는지 구하는 프로그램을 작성하세요. +단 중복값은 존재하지 않습니다. +""" + +import sys +sys.stdin = open('input.txt', 'r') +n, m = map(int, input().split(' ')) +ls = list(map(int, input().split(' '))) +ls.sort() + +lt = 0 # 왼쪽 인덱스 +rt = n-1 # 오른쪽 인덱스 +while lt <= rt: + mid = (lt+rt)//2 # 가운데 인덱스 + if ls[mid] > m: # 우리가 찍은 값이 원하는 값보다 오른쪽에 있으면 + rt = mid-1 # rt를 업데이트 해서 왼쪽을 검색하도록 함 + elif ls[mid] < m: # 우리가 찍은 값이 원하는 값보다 왼쪽에 있으면 + lt = mid+1 # lt를 업데이트 해서 오른쪽을 검색하도록 함 + else: # 우리가 찍은 값이 원하는 값이면 + print(mid+1) # 출력 + break + +""" +import sys +sys.stdin = open('input.txt', 'r') + +n, m = map(int, input().split(' ')) +ls = list(map(int, input().split(' '))) +ls.sort() +for idx, scalar in enumerate(ls): + if scalar == m: + print(idx+1) +""" \ No newline at end of file diff --git "a/section_4/Q2/Section4_Q2_\352\271\200\354\261\204\355\230\225.py" "b/section_4/Q2/Section4_Q2_\352\271\200\354\261\204\355\230\225.py" new file mode 100644 index 0000000..0d9047d --- /dev/null +++ "b/section_4/Q2/Section4_Q2_\352\271\200\354\261\204\355\230\225.py" @@ -0,0 +1,35 @@ +""" +Section 4 - Q2 + +랜선자르기(결정알고리즘) +""" + +import sys +sys.stdin = open('input.txt', 'r') + +k, n = map(int, input().split(' ')) +lans = [] +for i in range(k): + lans.append(int(input())) +lans.sort() + +res = 0 # 정답 +lt = 1 # 가장 짧은 길이 +rt = max(lans) # 가장 긴 길이 + +def Count(x): + cnt = 0 + for lan in lans: # 모든 랜선에 대하여 반복 + cnt += lan//x # 랜선을 자른 개수를 업데이트 + return cnt # 개수를 반환 + +while lt<=rt: + total = 0 + mid = (lt+rt)//2 # 가운데 길이 + if Count(mid) >= n: # 우리가 찍은 개수가 n보다 크거나 같으면 + res = mid # 해당 개수를 정답으로 업데이트 + lt = mid+1 # lt를 업데이트 하여 오른쪽을 검색하도록 함 + else: # 우리가 찍은 개수가 n보다 작으면 + rt = mid-1 # rt를 업데이트 하여 왼쪽을 검색하도록 함 + +print(res) \ No newline at end of file diff --git "a/section_4/Q3/Section4_Q3_\352\271\200\354\261\204\355\230\225.py" "b/section_4/Q3/Section4_Q3_\352\271\200\354\261\204\355\230\225.py" new file mode 100644 index 0000000..11476d5 --- /dev/null +++ "b/section_4/Q3/Section4_Q3_\352\271\200\354\261\204\355\230\225.py" @@ -0,0 +1,36 @@ +""" +Section 4 - Q3 + +뮤직비디오(결정알고리즘) +""" + +import sys +sys.stdin=open("input.txt", "r") + +n, m = map(int, input().split(' ')) +Music = list(map(int, input().split(' '))) + +def Count(capacity): # 우리가 찍은 길이를 받음 + cnt = 1 # DVD 개수 + sum = 0 # 현재 DVD에 저장된 곡의 길이 + for x in Music: + if sum+x>capacity: # 현재 DVD에 저장된 곡의 길이가 우리가 찍은 길이를 넘어가면 + cnt += 1 # DVD 개수 1 증가 + sum = x # 현재 DVD에 저장된 곡의 길이를 새롭게 업데이트 + else: # 현재 DVD에 저장된 곡의 길이가 우리가 찍은 길이를 넘어가지 않으면 + sum += x # 현재 DVD에 곡의 길이 계속하여 저장 + return cnt # DVD 개수 리턴 + +res = 0 # 정답 (DVD 길이 중 최소값) +lt = 1 # 가장 짧은 DVD 길이 +rt = sum(Music) # 가장 긴 DVD 길이 -> 즉 DVD 1개에 모든 곡을 담을 때의 DVD 길이 +# print(rt) + +while lt<=rt: + mid=(lt+rt)//2 # 가운데 길이 + if mid>=max(Music) and Count(mid)<=m: # 우리가 찍은 DVD 개수가 m보다 작거나 같으면 +) 가장 긴 곡 길이보다는 크거나 같아야 함! + res = mid # 정답 업데이트 + rt = mid-1 # rt를 업데이트 해서 DVD 길이가 작은 쪽을 검색하도록 함 + else: # 우리가 찍은 DVD 개수가 m보다 크면 + lt = mid+1 # lt를 업데이트 해서 DVD 길이가 큰 쪽을 검색하도록 함 +print(res) \ No newline at end of file diff --git "a/section_4/Q7/Section4_Q7_\352\271\200\354\261\204\355\230\225.py" "b/section_4/Q7/Section4_Q7_\352\271\200\354\261\204\355\230\225.py" new file mode 100644 index 0000000..1f9ed3c --- /dev/null +++ "b/section_4/Q7/Section4_Q7_\352\271\200\354\261\204\355\230\225.py" @@ -0,0 +1,22 @@ +""" +Section 4 - Q7 + +창고 정리 +""" + +import sys +sys.stdin = open('input.txt', 'r') + +l = int(input()) +height = list(map(int, input().split(' '))) +m = int(input()) + +for i in range(m): + + max_idx = height.index(max(height)) + min_idx = height.index(min(height)) + + height[max_idx] = height[max_idx]-1 + height[min_idx] = height[min_idx]+1 + +print(max(height) - min(height)) \ No newline at end of file From cb78a02a1f32983714561a7ab72efa375311ae10 Mon Sep 17 00:00:00 2001 From: cheris8 Date: Tue, 15 Sep 2020 16:04:00 +0900 Subject: [PATCH 3/4] =?UTF-8?q?=EA=B9=80=EC=B1=84=ED=98=95=20Section=206?= =?UTF-8?q?=20=EC=97=85=EB=A1=9C=EB=93=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...1_\352\271\200\354\261\204\355\230\225.py" | 21 +++++++++++ ...3_\352\271\200\354\261\204\355\230\225.py" | 30 ++++++++++++++++ ...6_\352\271\200\354\261\204\355\230\225.py" | 34 ++++++++++++++++++ ...8_\352\271\200\354\261\204\355\230\225.py" | 34 ++++++++++++++++++ ...9_\352\271\200\354\261\204\355\230\225.py" | 35 +++++++++++++++++++ 5 files changed, 154 insertions(+) create mode 100644 "section_6/Q1/Section6_Q1_\352\271\200\354\261\204\355\230\225.py" create mode 100644 "section_6/Q3/Section6_Q3_\352\271\200\354\261\204\355\230\225.py" create mode 100644 "section_6/Q6/Section6_Q6_\352\271\200\354\261\204\355\230\225.py" create mode 100644 "section_6/Q8/Section6_Q8_\352\271\200\354\261\204\355\230\225.py" create mode 100644 "section_6/Q9/Section6_Q9_\352\271\200\354\261\204\355\230\225.py" diff --git "a/section_6/Q1/Section6_Q1_\352\271\200\354\261\204\355\230\225.py" "b/section_6/Q1/Section6_Q1_\352\271\200\354\261\204\355\230\225.py" new file mode 100644 index 0000000..3dc8a6c --- /dev/null +++ "b/section_6/Q1/Section6_Q1_\352\271\200\354\261\204\355\230\225.py" @@ -0,0 +1,21 @@ +""" +Section 6 - Q1 + +재귀함수를 이용한 이진수 출력 + +10진수 N이 입력되면 2진수로 변환하여 출력하는 프로그램을 작성하세요. +단 재귀함수를 이용해서 출력해야 합니다. +""" +import sys +sys.stdin = open('input.txt', 'r') +n = int(input()) + +def Convert10to2(x): + if x==0: + return + else: + Convert10to2(x//2) # 몫 + print(x%2, end='') # 나머지 + +if __name__=='__main__': + Convert10to2(n) diff --git "a/section_6/Q3/Section6_Q3_\352\271\200\354\261\204\355\230\225.py" "b/section_6/Q3/Section6_Q3_\352\271\200\354\261\204\355\230\225.py" new file mode 100644 index 0000000..1b54dcf --- /dev/null +++ "b/section_6/Q3/Section6_Q3_\352\271\200\354\261\204\355\230\225.py" @@ -0,0 +1,30 @@ +""" +Section 6 - Q3 + +부분집합 구하기(DFS) + +자연수 N이 주어지면 1부터 N까지의 원소를 갖는 집합의 부분집합을 모두 출력하는 프로그램 을 작성하세요. +""" + +import sys +sys.stdin = open('input.txt', 'r') + +def DFS(d): + # 종착점 즉 터미널 노드일 때 + if d==n+1: # d==4일 때 + # 체크 변수에서 값이 1인 원소들만 출력 + for i in range(1, n+1): + if ch[i]==1: + print(i, end=' ') + print() + # 상태트리 탐색 + else: + ch[d] = 1 # 원소를 사용하겠다! 하여 1을 값으로 할당 + DFS(d+1) # 가지 뻗기 ㄱ + ch[d] = 0 # 원소를 사용하지 않겠다! 하여 0을 값으로 할당 + DFS(d+1) # 가지 뻗기 ㄱ + +if __name__=='__main__': + n = int(input()) # n=3 + ch = [0] * (n+1) # ch=[0,0,0,0] : 원소를 사용하겠다! 사용하지 않겠다!를 1, 0으로 체크할 체크 변수 + DFS(1) diff --git "a/section_6/Q6/Section6_Q6_\352\271\200\354\261\204\355\230\225.py" "b/section_6/Q6/Section6_Q6_\352\271\200\354\261\204\355\230\225.py" new file mode 100644 index 0000000..b4f49cc --- /dev/null +++ "b/section_6/Q6/Section6_Q6_\352\271\200\354\261\204\355\230\225.py" @@ -0,0 +1,34 @@ +""" +Section 6 -Q6 + +중복순열 구하기 + +1부터 N까지 번호가 적힌 구슬이 있습니다. +이 중 중복을 허락하여 M번을 뽑아 일렬로 나열하는 방법을 모두 출력합니다. +""" + +import sys +sys.stdin = open('input.txt', 'r') + +def DFS(d): # d=0,1,2 : 상태트리의 depth, 그리고 res의 인덱스 + global cnt + # 종착점 즉 터미널 노드일 때 + if d==m: + # res의 원소들을 옆으로 출력 + for i in range(m): + print(res[i], end=' ') + print() + # 정답 개수 1 증가 + cnt += 1 + # 상태트리 탐색 + else: + for i in range(1, n+1): # i=1,2,3 + res[d] = i + DFS(d+1) + +if __name__=='__main__': + n, m = map(int, input().split(' ')) # n=3, m=2 + res = [0] * m # res=[0,0] : 출력하고 싶은 정답값 + cnt = 0 + DFS(0) + print(cnt) diff --git "a/section_6/Q8/Section6_Q8_\352\271\200\354\261\204\355\230\225.py" "b/section_6/Q8/Section6_Q8_\352\271\200\354\261\204\355\230\225.py" new file mode 100644 index 0000000..99d5acf --- /dev/null +++ "b/section_6/Q8/Section6_Q8_\352\271\200\354\261\204\355\230\225.py" @@ -0,0 +1,34 @@ +""" +Section 6 - Q8 + +순열 구하기 + +1부터 N까지 번호가 적힌 구슬이 있습니다. +이중 M개를 뽑아 일렬로 나열하는 방법을 모두 출력합니다. +""" + +import sys +sys.stdin = open('input.txt', 'r') + +def DFS(x): + global cnt + if x==m: + for i in res: + print(i, end=' ') + print() + cnt += 1 + else: + for i in range(1, n+1): # i=1,2,3 + if ch[i]==0: + res[x] = i + ch[i] = 1 # 1을 선택했을 때 또다시 1을 선택하지 않도록 1로 체크 + DFS(x+1) + ch[i] = 0 # 1을 다 돌고 나면 2,3을 돌 때에는 1을 사용할 수 있도록 0으로 체크 + +if __name__=='__main__': + n, m = map(int, input().split(' ')) + res = [0] * m + ch = [0] * (n+1) # 체크 변수 + cnt = 0 + DFS(0) + print(cnt) diff --git "a/section_6/Q9/Section6_Q9_\352\271\200\354\261\204\355\230\225.py" "b/section_6/Q9/Section6_Q9_\352\271\200\354\261\204\355\230\225.py" new file mode 100644 index 0000000..60abf15 --- /dev/null +++ "b/section_6/Q9/Section6_Q9_\352\271\200\354\261\204\355\230\225.py" @@ -0,0 +1,35 @@ +""" +Section 6 - Q9 + +수열 추측하기 +""" + +import sys +sys.stdin = open('input.txt', 'r') + +def DFS(x, total): + + # 종착점 + 값이 일치할 때 + if x == n and total == f: + for ele in seq: + print(ele, end=' ') + sys.exit() # 코드 종료 + + else: + for i in range(1, n+1): # i=1,2,3,4 + if ch[i] == 0: + ch[i] = 1 # 사용했다! 로 체크 + seq[x] = i # 수열 만들기 + tot = seq[x]*b[x] + DFS(x+1, total+tot) + ch[i] = 0 # 사용 안 했다! 로 초기화 + +if __name__ == "__main__": + n, f = map(int, input().split()) # n=4, f=16 + seq = [0] * n # seq = [0,0,0,0] : 수열 초기화 + b = [1] * n # b = [1,1,1,1] : 계수 초기화 + for i in range(1, n): + b[i] = int(b[i-1] * (n-i)/i) + #print(b) + ch = [0] * (n+1) # 체크 변수 + DFS(0, 0) # b[1]*seq[1] + From 6660ae48763d862c71b716d7a9e665c81c57a534 Mon Sep 17 00:00:00 2001 From: cheris8 Date: Tue, 22 Sep 2020 16:07:32 +0900 Subject: [PATCH 4/4] =?UTF-8?q?=EA=B9=80=EC=B1=84=ED=98=95=20Section=207?= =?UTF-8?q?=20=EC=97=85=EB=A1=9C=EB=93=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...0_\352\271\200\354\261\204\355\230\225.py" | 37 +++++++++++++++++ ...2_\352\271\200\354\261\204\355\230\225.py" | 18 +++++++++ ...1_\352\271\200\354\261\204\355\230\225.py" | 40 +++++++++++++++++++ ...2_\352\271\200\354\261\204\355\230\225.py" | 35 ++++++++++++++++ ...3_\352\271\200\354\261\204\355\230\225.py" | 26 ++++++++++++ 5 files changed, 156 insertions(+) create mode 100644 "section_6/Q10/Section6_Q10_\352\271\200\354\261\204\355\230\225.py" create mode 100644 "section_6/Q12/Section6_Q12_\352\271\200\354\261\204\355\230\225.py" create mode 100644 "section_7/Q1/Section7_Q1_\352\271\200\354\261\204\355\230\225.py" create mode 100644 "section_7/Q2/Section7_Q2_\352\271\200\354\261\204\355\230\225.py" create mode 100644 "section_7/Q3/Section7_Q3_\352\271\200\354\261\204\355\230\225.py" diff --git "a/section_6/Q10/Section6_Q10_\352\271\200\354\261\204\355\230\225.py" "b/section_6/Q10/Section6_Q10_\352\271\200\354\261\204\355\230\225.py" new file mode 100644 index 0000000..359dab1 --- /dev/null +++ "b/section_6/Q10/Section6_Q10_\352\271\200\354\261\204\355\230\225.py" @@ -0,0 +1,37 @@ +""" +Section 6 - Q10 + +조합 구하기 + +1부터 N까지 번호가 적힌 구슬이 있습니다. +이중 M개를 뽑는 방법의 수를 출력하는 프로그램을 작성하세요. +""" + +import sys +sys.stdin = open('input.txt', 'r') + +def DFS(d, s): + + global cnt + + if d==m: + for ele in res: + print(ele, end=' ') + print() + cnt += 1 + + else: + for i in range(s, n+1): + if ch[i]==0: + res[d] = i + ch[i] = 1 + DFS(d+1, i+1) + ch[i] = 0 + +if __name__=='__main__': + n, m = map(int, input().split(' ')) # n=4, m=2 + res = [0] * m + ch = [0] * (n+1) + cnt = 0 + DFS(0, 1) + print(cnt) diff --git "a/section_6/Q12/Section6_Q12_\352\271\200\354\261\204\355\230\225.py" "b/section_6/Q12/Section6_Q12_\352\271\200\354\261\204\355\230\225.py" new file mode 100644 index 0000000..1d3d00d --- /dev/null +++ "b/section_6/Q12/Section6_Q12_\352\271\200\354\261\204\355\230\225.py" @@ -0,0 +1,18 @@ +""" +Section 6 - Q12 + +인접행렬(가중치 방향그래프) +""" + +import sys +sys.stdin = open('input.txt', 'r') +n, m = map(int, input().split(' ')) +g = [[0]*n for _ in range(n)] # 인접행렬 초기화 +for _ in range(m): + a, b, v = map(int, input().split(' ')) + g[a-1][b-1] = v +#print(g) +for i in range(n): + for j in range(n): + print(g[i][j], end=' ') + print() diff --git "a/section_7/Q1/Section7_Q1_\352\271\200\354\261\204\355\230\225.py" "b/section_7/Q1/Section7_Q1_\352\271\200\354\261\204\355\230\225.py" new file mode 100644 index 0000000..ac62380 --- /dev/null +++ "b/section_7/Q1/Section7_Q1_\352\271\200\354\261\204\355\230\225.py" @@ -0,0 +1,40 @@ +""" +Section 7 - Q1 + +최대점수 구하기(DFS) + +이번 정보올림피아드대회에서 좋은 성적을 내기 위하여 현수는 선생님이 주신 N개의 문제를 풀려고 합니다. +각 문제는 그것을 풀었을 때 얻는 점수와 푸는데 걸리는 시간이 주어지게 됩니다. +제한시간 M안에 N개의 문제 중 최대점수를 얻을 수 있도록 해야 합니다. +(해당문제는 해당시간이 걸리면 푸는 걸로 간주한다, 한 유형당 한개만 풀 수 있습니다.) +""" + +import sys +sys.stdin = open('input.txt', 'r') + +def DFS(d, score, time): # d = 0,1,2,3,4 => 문제 1,2,3,4,5 + + global res + + if time > m: # 시간 초과하면 + return # 가지치기 + + if d==n: # 종착점 + if score>res: + res = score + + else: + DFS(d+1, score+score_ls[d], time+time_ls[d]) # d번째 문제를 풀고 넘긴다! + DFS(d+1, score, time) # d번째 문제를 안 풀고 넘긴다! + +if __name__=='__main__': + n, m = map(int, input().split(' ')) # n=5, m=20(제한시간) + score_ls = [] + time_ls = [] + for _ in range(n): + a, b = map(int, input().split(' ')) # 점수, 시간 + score_ls.append(a) + time_ls.append(b) + res = float('-inf') + DFS(0, 0, 0) + print(res) diff --git "a/section_7/Q2/Section7_Q2_\352\271\200\354\261\204\355\230\225.py" "b/section_7/Q2/Section7_Q2_\352\271\200\354\261\204\355\230\225.py" new file mode 100644 index 0000000..cdec452 --- /dev/null +++ "b/section_7/Q2/Section7_Q2_\352\271\200\354\261\204\355\230\225.py" @@ -0,0 +1,35 @@ +""" +Section 7 - Q2 + +휴가(삼성 SW역량평가 기출문제 : DFS활용) +""" + +import sys +sys.stdin = open('input.txt', 'r') + +def DFS(d, total): + global res + # 종착점 (T, P에 맨앞에 0 삽입했으므로 n+1까지 가야 터미널 노드임) + if d==n+1: + if total>res: + res = total + else: + # d일 상담을 하는 경우 + if d+T[d] <= n+1: # T, P에 맨앞에 0 삽입했으므로 n+1로 해야 7일까지 읽을 수 있음 + DFS(d+T[d], total+P[d]) + # d일 상담 하지 않는 경우 + DFS(d+1, total) + +if __name__=='__main__': + n = int(input()) # n=7 + T = list() + P = list() + for _ in range(n): + t, p = map(int, input().split()) + T.append(t) + P.append(p) + T.insert(0, 0) # 1일을 인덱스 1로 맞춰주기 위해서 + P.insert(0, 0) # 1일을 인덱스 1로 맞춰주기 위해서 + res = 0 # 정답 + DFS(1, 0) + print(res) diff --git "a/section_7/Q3/Section7_Q3_\352\271\200\354\261\204\355\230\225.py" "b/section_7/Q3/Section7_Q3_\352\271\200\354\261\204\355\230\225.py" new file mode 100644 index 0000000..0a73cfc --- /dev/null +++ "b/section_7/Q3/Section7_Q3_\352\271\200\354\261\204\355\230\225.py" @@ -0,0 +1,26 @@ +""" +Section 7 - Q3 + +양팔저울(DFS) +""" + +import sys +sys.stdin = open('input.txt', 'r') + +def DFS(d, total): + # 종착점 + if d==k: + if 0 < total <= s: # total이 0~s 사이면 + res.add(total) # 가능한 것으로 추가 + else: + DFS(d+1, total) # d번째 추를 사용하지 않음 + DFS(d+1, total+chu[d]) # d번째 추를 더함 + DFS(d+1, total-chu[d]) # d번째 추를 뺌 + +if __name__=='__main__': + k = int(input()) + chu = list(map(int, input().split())) + s = sum(chu) + res = set() # 가능한 물 + DFS(0, 0) + print(s - len(res))