알고리즘/브루트 포스
백준 - 6603 - 로또
easysheep
2023. 2. 2. 00:30
문제 출처
https://www.acmicpc.net/problem/6603
6603번: 로또
입력은 여러 개의 테스트 케이스로 이루어져 있다. 각 테스트 케이스는 한 줄로 이루어져 있다. 첫 번째 수는 k (6 < k < 13)이고, 다음 k개 수는 집합 S에 포함되는 수이다. S의 원소는 오름차순으로
www.acmicpc.net
풀이
1번 itertools를 이용
import sys
from itertools import combinations
#def list_int_input():
# return [int(i) for i in input().split(" ")]
while True:
input_list = list(map(int,sys.stdin.readline().split()))
if len(input_list)==1:
break
input_list.pop(0)
for com in combinations(input_list,6):
temp = list(map(str,com))
print(" ".join(temp))
print()
2번 재귀 함수 사용 깊이 우선 탐색
def print_lotto(idx, depth):
if depth == 6:
print(*result)
else:
for i in range(idx,list_len):
result.append(input_list[i])
print_lotto(i +1,depth+1)
result.pop()
while True:
input_list = input().split()
if len(input_list) == 1:
break
result = []
list_len = int(input_list[0])
input_list = input_list[1:]
print_lotto(0,0)
print()