문제 링크: https://www.acmicpc.net/problem/15656
15656번: N과 M (7)
N개의 자연수와 자연수 M이 주어졌을 때, 아래 조건을 만족하는 길이가 M인 수열을 모두 구하는 프로그램을 작성하시오. N개의 자연수는 모두 다른 수이다. N개의 자연수 중에서 M개를 고른 수열 같은 수를 여러 번 골라도 된다.
www.acmicpc.net
1부터 n까지가 아닌, 주어진 숫자들을 사용하여 중복 순열을 출력하는 문제이다.
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
|
#include <bits/stdc++.h>
using namespace std;
int n, m, a[10], ans[10];
void solve(int idx) {
if(idx == m) {
for(int i = 0; i < m; i++)
cout << ans[i] << ' ';
cout << '\n';
return;
}
for(int i = 0; i < n; i++) {
ans[idx] = a[i];
solve(idx + 1);
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m;
for(int i = 0; i < n; i++) cin >> a[i];
sort(a, a + n);
solve(0);
return 0;
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
|
'재귀, 백트래킹 (Recursion, Backtracking)' 카테고리의 다른 글
백준 10972 다음 순열 (0) | 2020.05.04 |
---|---|
백준 15657 N과 M (8) (0) | 2020.05.04 |
백준 15655 N과 M (6) (0) | 2020.05.04 |
백준 15654 N과 M (5) (0) | 2020.05.04 |
백준 15652 N과 M (4) (0) | 2020.05.03 |