Skip to content

Commit

Permalink
[Bronze I] Title: 일곱 난쟁이, Time: 312 ms, Memory: 58388 KB -BaekjoonHub
Browse files Browse the repository at this point in the history
  • Loading branch information
ddubbu-dev committed Aug 10, 2024
1 parent 27523b9 commit b31703d
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 0 deletions.
32 changes: 32 additions & 0 deletions 백준/Bronze/2309. 일곱 난쟁이/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# [Bronze I] 일곱 난쟁이 - 2309

[문제 링크](https://www.acmicpc.net/problem/2309)

### 성능 요약

메모리: 58388 KB, 시간: 312 ms

### 분류

브루트포스 알고리즘, 정렬

### 제출 일자

2024년 8월 10일 17:58:59

### 문제 설명

<p>왕비를 피해 일곱 난쟁이들과 함께 평화롭게 생활하고 있던 백설공주에게 위기가 찾아왔다. 일과를 마치고 돌아온 난쟁이가 일곱 명이 아닌 아홉 명이었던 것이다.</p>

<p>아홉 명의 난쟁이는 모두 자신이 "백설 공주와 일곱 난쟁이"의 주인공이라고 주장했다. 뛰어난 수학적 직관력을 가지고 있던 백설공주는, 다행스럽게도 일곱 난쟁이의 키의 합이 100이 됨을 기억해 냈다.</p>

<p>아홉 난쟁이의 키가 주어졌을 때, 백설공주를 도와 일곱 난쟁이를 찾는 프로그램을 작성하시오.</p>

### 입력

<p>아홉 개의 줄에 걸쳐 난쟁이들의 키가 주어진다. 주어지는 키는 100을 넘지 않는 자연수이며, 아홉 난쟁이의 키는 모두 다르며, 가능한 정답이 여러 가지인 경우에는 아무거나 출력한다.</p>

### 출력

<p>일곱 난쟁이의 키를 오름차순으로 출력한다. 일곱 난쟁이를 찾을 수 없는 경우는 없다.</p>

51 changes: 51 additions & 0 deletions 백준/Bronze/2309. 일곱 난쟁이/일곱 난쟁이.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
"""
[문제 이해]
- 9명 중 7명 키 합을 구해서 = 100인 경우
- 난쟁이들 키 오름차순 출력
[풀이]
- 조합으로 9C7 경우의 수 찾기
"""

N = 9
arr = []
for i in range(N):
arr.append(int(input()))

# 탐색 -

def get순열_중복없음(storage_nums, k, picked_nums, search_idx):
collections = [] # 값 생성을 위해 클로저 정의

def recur(picked_nums, search_idx):
if search_idx >= len(storage_nums):
return

if len(picked_nums) == k: # 재귀 - 탈출
collections.append(picked_nums[:]) # 주의) 리스트 참조값
return

picked_num = storage_nums[search_idx]

if storage_nums[search_idx] not in picked_nums:
picked_nums.append(picked_num)
recur(picked_nums, 0)

# 재귀 - 복구
picked_nums.pop()

# 재귀 - 호출
recur(picked_nums, search_idx + 1) # 그 다음부터 찾기

recur(picked_nums, search_idx)
return collections


collections = get순열_중복없음(arr, 7, [], 0)

for case in collections:
acc = sum(case)

if acc == 100:
print("\n".join(map(str, sorted(case))))
break

0 comments on commit b31703d

Please sign in to comment.