BAEKJOON/permutations & combinations

[baekJoon6603] 로또(순열/조합 정리)

ferozsun 2023. 10. 25. 14:44

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

 

6603번: 로또

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

www.acmicpc.net


* 순열 Permutations : 순서 다르면 다른 것으로 판단

from itertools import permutations

list(permutations([1, 2, 3, 4], 2))
# [(1, 2), (1, 3), (1, 4), (2, 1), (2, 3), (2, 4), (3, 1), (3, 2), (3, 4), (4, 1), (4, 2), (4, 3)]

* 조합 Combinations: 순서 신경 X

from itertools import combinations

print(list(combinations([1,2,3,4], 2)))
# [(1, 2), (1, 3), (1, 4), (2, 3), (2, 4), (3, 4)]

* Product

from itertools import product

print(list(product([1,2,3,4], repeat=2)))
# [(1, 1), (1, 2), (1, 3), (1, 4), (2, 1), (2, 2), (2, 3), (2, 4), (3, 1), (3, 2), (3, 3), (3, 4), (4, 1), (4, 2), (4, 3), (4, 4)]

import sys
from itertools import combinations
# sys.stdin.readline

while True:
    s = sys.stdin.readline()
    if s == '0\n':
        break

    l = s.split()[1:]

    for i in list(combinations(l, 6)): # 조합
        print(*i) # list 한 줄 출력

    print('')