모든 경우를 탐색하는 백트래킹 알고리즘
itertools / permutations / combinations
2021.05.26 - [파이썬 Python] - [파이썬 Python] itertools , 순열과 조합 (permutations, combinations)
join
2021.05.26 - [파이썬 Python] - [파이썬 Python] split / join 함수 : 리스트의 문자열 합치기 나누기
N과 M (1)
내 코드
from itertools import permutations
N, M = map(int, input().split())
P = permutations(range(1, N+1), M)
for p in P :
print(' '.join(map(str, p)))
N과 M (2)
내 코드
from itertools import combinations
n, m = map(int, input().split())
c = combinations(range(1,n+1), m)
for i in c :
print(' '.join(map(str, i)))
N과 M (3)
내 코드
from itertools import product
n, m = map(int, input().split())
P = product(range(1, n+1), repeat=m)
for p in P:
print(" ".join(map(str, p)))
N과 M (4)
내 코드
from itertools import combinations_with_replacement
n, m = map(int, input().split())
C = combinations_with_replacement(range(1, n+1), m)
for c in C:
print(" ".join(map(str, c)))
출처
https://www.acmicpc.net/problem/15649
15649번: N과 M (1)
한 줄에 하나씩 문제의 조건을 만족하는 수열을 출력한다. 중복되는 수열을 여러 번 출력하면 안되며, 각 수열은 공백으로 구분해서 출력해야 한다. 수열은 사전 순으로 증가하는 순서로 출력해
www.acmicpc.net
'코딩테스트 > 백준' 카테고리의 다른 글
[백준] 2606번 : 바이러스 / DFS / 파이썬 Python (0) | 2021.06.15 |
---|---|
[백준] 1260번 : DFS와 BFS / 파이썬 Python (0) | 2021.06.10 |
[백준] 13305번 : 주유소 / 파이썬 Python / 그리디(Greedy) 알고리즘 (0) | 2021.05.26 |
[백준] 1541번 : 잃어버린 괄호 / 그리디(Greedy) 알고리즘 / 파이썬 Python (0) | 2021.05.24 |
[백준] 11399번 : ATM / 그리디(Greedy) 알고리즘 / 파이썬 Python (0) | 2021.05.23 |