코딩딩딩

(python) 백준 6603 - 로또 본문

백준

(python) 백준 6603 - 로또

komizke 2023. 1. 23. 00:00

백준 6603

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

 

6603번: 로또

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

www.acmicpc.net

 

1. 문제 설명

 

입력받은 수들 중에서 만들 수 있는 로또 번호의 모든 경우의 수를 출력하는 문제

 

2. 문제 풀이

 

번호들을 리스트에 저장하고 combinations함수를 이용하여 6개를 선택하는 모든 경우의 수를 저장한다.

 

numbers = list(input().split())
combination_numbers_list = combinations(numbers[1:len(numbers)], 6)	#로또 번호 선택

 

 

combinations 관련 설명: https://michelangeloo.tistory.com/10 

 

(python) 순열, 조합, 중복순열, 중복조합 구현(Feat. itertools)

1. 순열 n​Pr​: 서로 다른 n개에서 r개를 중복 없이 나열하는 경우의 수 itertools 라이브러리에서permutations 함수 활용 from itertools import permutations 사용예시 1) 'A', 'B', 'C'에서 두 개를 중복 없이 선택

michelangeloo.tistory.com

 

위에서 저장한 데이터를 출력할 때 join함수를 이용하여 한 칸씩 띄어서 출력한다.

 

for number_list in combination_numbers_list:
    print(' '.join(number_list))

 

 

join함수 관련 설명:https://michelangeloo.tistory.com/3

 

(python) 파이썬 join 함수 정리(Feat.백준 16943)

1. join 함수 기본적으로 리스트를 출력 시 예를 들어 ['1', '2', '3'] 이런 식으로 ','을 포함하여 출력이 된다. 각 원소값만 붙여서 하나의 수로 나타내고 싶었을 때가 있었을 것이다. 이런 경우에 구

michelangeloo.tistory.com

 

3. 전체 코드

 

from itertools import combinations
while True:
    numbers = list(input().split())	#한 줄에 입력받기
    if '0' in numbers:
        break
    combination_numbers_list = combinations(numbers[1:len(numbers)], 6)	#6개 조합 선택
    for number_list in combination_numbers_list:
        print(' '.join(number_list))	#띄어 쓰면서 출력
    print()

'백준' 카테고리의 다른 글

(python) 백준 1912 - 연속합  (0) 2023.01.28
(python) 백준 1759 - 암호 만들기  (1) 2023.01.25
(python) 백준 1546 - 평균  (0) 2023.01.21
(python) 백준 1157 - 단어 공부  (0) 2023.01.21
(python) 백준 2581 - 소수  (0) 2023.01.21
Comments