티스토리 뷰

728x90
반응형

 

https://www.acmicpc.net/problem/6603

 

6603번: 로또

입력은 여러 개의 테스트 케이스로 이루어져 있다. 각 테스트 케이스는 한 줄로 이루어져 있다. 첫 번째 수는 k (6 < k < 13)이고, 다음 k개 수는 집합 S에 포함되는 수이다. S의 원소는 오름차순으로

www.acmicpc.net

 

 


 

 

<나의 풀이>

# 조합 모듈 추가
from itertools import combinations

# 테스트 케이스
T = []

# 0이면 종료, 아니면 값 추가
while True:
    s = list(map(int, input().split()))
    if len(s) == 1:
        break
    else:
        T.append(s[1:])

# 중간에 줄바꿈을 위해 넣은 변수
block = 0

# 조합 출력
for i in T:
    if block != 0:
        print(" ")
    s.sort()
    combin = list(combinations(i, 6))
    for j in range(len(combin)):
        for k in range(len(combin[j])):
            print(combin[j][k], end=" ")
        print()
    block = 1

 

 

<다른 사람의 풀이>

# kimwooa23님 코드

def solve(index, depth):
    if depth == 6:
        print(*result)
        return

    for i in range(index, len(lst)):
        result.append(lst[i])
        solve(i+1, depth+1)
        result.pop()


while 1:
    lst = list(map(int, input().split()))
    if lst[0] == 0:
        break
    del lst[0]
    result = []
    solve(0, 0)
    print()
    
    
# <배운점> 
# => 재귀로 푼 점

 

# park345601님 코드

import sys
from itertools import combinations

IN = sys.stdin.readline().split()
while IN[0] != '0':
    cm = combinations(IN[1:], 6)
    for c in cm:
        print(' '.join(c))
    print()
    IN = sys.stdin.readline().split()
    

# <배운점> 
# => 코드를 간략하게 짠 점
728x90
반응형
댓글