-
[Python] 백준 15650 N과 M (2)Python_알고리즘 2021. 8. 3. 01:47
문제 바로가기
문제분석
# 백트래킹 = DFS(재귀) + 가지치기(break or continue)
# 모든 경우의 수를 탐색하면서 break나 continue로 가지치기를 해 경우의 수를 줄이는 방법# 순열 공식 : nPr = n!/(n-r)!
# 조합 공식 : nCr = nPr/r!
문제풀이
from itertools import combinations N, M = map(int, input().split()) arr = [i for i in range(1, N+1)] answer = list(combinations(arr, M)) for element in answer: for element2 in element: print(element2, end=" ") print()
# 자연수 n, m을 입력값으로 받는다.
# 1부터 n까지 자연수 중에서 각 값을 뽑아 리스트에 저장한다.
# 해당 리스트에서 combinations을 통해 조합을 만든다.
# combinations 사용법 arr = [1, 2, 3] a = combinations(arr, 2) print(list(a)) # [(1,2),(1,3),(2,3)]
- arr에서 m개의 조합을 구한다.
'Python_알고리즘' 카테고리의 다른 글
[Python] 백준 2579 계단 오르기 (0) 2021.08.05 [Python] 백준 9663 N-Queen (0) 2021.08.04 [Python] 백준 2108 통계학 (0) 2021.07.31 [Python] 백준 1260 DFS와 BFS (0) 2021.07.30 [Python] 백준 1874 스택 수열 (0) 2021.07.27