일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 백트래킹
- MyPlaylist
- 모각코
- Planned
- 장고
- SQL
- 프로그래머스
- Bellman-Ford
- 그리디알고리즘
- 동적계획법
- 최소스패닝트리
- DFS
- 알고리즘
- 백준
- minimum spanning tree
- 함밥
- codetree
- 종합설계
- BFS
- django
- Kruskal
- 마라마라빔
- B대면노래방
- 실습
- 데이터베이스
- 소프트웨어공학
- DP
- 코드트리
- 파이썬
- programmers
Archives
- Today
- Total
Leta Learns
[Python] 백준 15656, 15657번 - N과 M (7), (8) 본문
N과 M (7)
https://www.acmicpc.net/problem/15656
15656번: N과 M (7)
N개의 자연수와 자연수 M이 주어졌을 때, 아래 조건을 만족하는 길이가 M인 수열을 모두 구하는 프로그램을 작성하시오. N개의 자연수는 모두 다른 수이다. N개의 자연수 중에서 M개를 고른 수열
www.acmicpc.net
앞서 풀었던 n과 m 문제들과 유사하나, 같은 수를 여러 번 골라도 되기 때문에 dfs 함수의 for문에서 if i not in ans: 조건문을 삭제해주면 된다.
import sys
input = sys.stdin.readline
def dfs():
if len(ans) == m:
print(*ans)
return
for i in num:
ans.append(i)
dfs()
ans.pop()
n, m = map(int, input().split())
num = list(map(int, input().split()))
num.sort()
ans = []
dfs()
N과 M (8)
https://www.acmicpc.net/problem/15657
15657번: N과 M (8)
N개의 자연수와 자연수 M이 주어졌을 때, 아래 조건을 만족하는 길이가 M인 수열을 모두 구하는 프로그램을 작성하시오. N개의 자연수는 모두 다른 수이다. N개의 자연수 중에서 M개를 고른 수열
www.acmicpc.net
고른 수열이 비내림차순이어야 하므로 시작하는 숫자를 정해서 그것보다 작은 숫자는 재귀 루프에 들어가지 않도록 한다.
import sys
input = sys.stdin.readline
def dfs(s): #s: start
if len(ans) == m:
print(*ans)
return
for i in num:
if i >= s:
ans.append(i)
dfs(i)
ans.pop()
n, m = map(int, input().split())
num = list(map(int, input().split()))
num.sort()
ans = []
dfs(0)
'Coding > 백준' 카테고리의 다른 글
[Python] 백준 15664번 - N과 M (10) (0) | 2022.02.26 |
---|---|
[Python] 백준 15663번 - N과 M (9) (0) | 2022.02.26 |
[Python] 백준 15655번 - N과 M (6) (0) | 2022.02.25 |
[Python] 백준 15652, 15654번 - N과 M (4), (5) (0) | 2022.02.25 |
[Python] 백준 15651번 - N과 M (3) (0) | 2022.02.25 |
Comments