🔑 알고리즘/백준 Python
백준15652번 파이썬 N과M4 #13-4) 백트래킹
Tamii
2020. 10. 20. 20:15
반응형
내 풀이
def back(d,n,m,k):
if d==m:
print(*ans)
return
for i in range(k,n):
ans.append(i+1)
back(d+1,n,m,i)
ans.pop()
n,m=map(int,input().split())
ans=[]
back(0,n,m,0)
최대한 라이브러리를 안써보겠다는 의지...?
depth 로 조건 걸 필요 없이,
i를 k로 넣어주면 되는 거였음!!!!!!!!!!
그럼 합리적인 라이브러리 사용법을 알아보자면~^^
내 풀이 2
from itertools import product
n,m=map(int,input().split())
num=[]
nnum=[]
ans=[]
for i in range(n):
num.append(i+1)
for i in range(m):
nnum.append(num)
ans=list(product(*nnum))
for i in ans:
if i[1]>=i[0]:
print(*i)
저번에 배웠던 product를 써먹기 위해 열심히 해서
배열 3개를 쓰는 (구질구질한)코드를 만들고 뿌듯해 하고 있었는데,,,
다른 풀이
from itertools import combinations_with_replacement
n,m = map(int,input().split())
ans=combinations_with_replacement(range(1,n+1),m)
for i in ans:
print(*i)
아니 이것도 라이브러리가 있다구요,,,.?
combinations_with_replacement( 범위, 원소개수)