본문 바로가기
Python3/백준 알고리즘

2022.02.11 [백준] (python 파이썬) N과 M (2)

by ian's coding 2022. 2. 11.
728x90
반응형

 

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

 

15650번: N과 M (2)

한 줄에 하나씩 문제의 조건을 만족하는 수열을 출력한다. 중복되는 수열을 여러 번 출력하면 안되며, 각 수열은 공백으로 구분해서 출력해야 한다. 수열은 사전 순으로 증가하는 순서로 출력해

www.acmicpc.net


풀이

이 문제는 조합문제이다.

itertools 를 이용하여 combinations를 사용할 수 있다.

combinations를 사용하면 nCm을 구할 수 있다.(n개에서 중복없이 m개 추출)

combinations(list,a)  list에서 a개를 뽑아내는 함수이다.

 

1
2
3
4
5
6
7
from itertools import combinations
 
a,b= list(map(int, input().split()))
arr =[str(i) for i in range(1,a+1)]
for i in list(combinations(arr,b)):
    print(" ".join(i))
 
cs

 

 

 

 

728x90
반응형

댓글