728x90
https://www.acmicpc.net/problem/15650
15650번: N과 M (2)
한 줄에 하나씩 문제의 조건을 만족하는 수열을 출력한다. 중복되는 수열을 여러 번 출력하면 안되며, 각 수열은 공백으로 구분해서 출력해야 한다. 수열은 사전 순으로 증가하는 순서로 출력해
www.acmicpc.net
말그대로 조합을 구하라는 문제였다.
combinations 함수로 풀어도됐지만
조합 구현하는 방법을 아는건 중요하다 생각하기 때문에
직접 구현도 해보고 combinations 로도 풀어보았다.
혹시 몰라 c++로 조합과 순열 구현한 풀이도 한번 첨부해봄..
알고리즘 공부 할때 c++로 시작했기때문에 ㅎㅎ..
https://foameraserblue.tistory.com/62?category=481823
조합과 순열 c++ 로 구현
https://yabmoons.tistory.com/99 https://yabmoons.tistory.com/100 저 위 블로그 주소에서 보고 알고리즘 공부중 써먹을곳이 많겠다 싶어서 작성합니다 백준 17281번 문제 푸는데 푸는법은 알겠어.. 근데 순열을..
foameraserblue.tistory.com
직접 구현한 풀이
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
import sys
input = sys.stdin.readline
n, m = map(int, input().split())
# 1~n 까지의 자연수중에 m개를 선택하는 조합 문제
visit = [0 for i in range(n + 1)]
def print_num():
for i in range(1, n + 1):
if visit[i] == 1:
print(i, end=' ')
print('')
def dfs(x, cnt):
if cnt == m:
print_num()
return
for i in range(x, n + 1):
if visit[i] == 1:
continue
visit[i] = 1
dfs(i, cnt + 1)
visit[i] = 0
dfs(1, 0)
|
cs |
combinations 함수를 사용한 풀이
1
2
3
4
5
6
7
8
9
10
11
12
|
import sys
from itertools import combinations
input = sys.stdin.readline
n, m = map(int, input().split())
# 1~n 까지의 자연수중에 m개를 선택하는 조합 문제
li = list(combinations(range(1, n + 1), m))
for l in li:
print(' '.join(map(str, l)))
|
cs |
728x90
'PS > 백준' 카테고리의 다른 글
[백준 2798번 ] 블랙잭 (파이썬/python) (0) | 2021.06.15 |
---|---|
[백준 1002번] 터렛 (파이썬/python) (0) | 2021.06.15 |
[백준 2630번] 색종이 만들기 (파이썬/python) (0) | 2021.06.15 |
[백준 2108번] 통계학 (파이썬/python) (0) | 2021.06.15 |
[백준 1874번] 스택 수열 (파이썬/python) (0) | 2021.06.15 |