Algorithm/BOJ
[BOJ]15651번: N과 M (3)(c++)
HBGB
2020. 5. 5. 11:29
https://www.acmicpc.net/problem/15651
15651번: N과 M (3)
한 줄에 하나씩 문제의 조건을 만족하는 수열을 출력한다. 중복되는 수열을 여러 번 출력하면 안되며, 각 수열은 공백으로 구분해서 출력해야 한다. 수열은 사전 순으로 증가하는 순서로 출력해야 한다.
www.acmicpc.net
|
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
32
33
34
35
36
37
38
39
40
|
#include <iostream>
using namespace std;
const int MAX = 7;
char output[MAX * 2 + 1];
void perm(int N, int M, int depth)
{
// depth가 M에 도달하면 출력
if (depth == M)
{
cout << output << '\n';
return;
}
// 중복을 허용하고 정렬 순서가 없는 수열 입력
for (int i = 1; i <= N; ++i)
{
output[depth * 2] = i + '0';
output[depth * 2 + 1] = ' ';
perm(N, M, depth + 1);
}
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
// 입력
int N, M;
cin >> N >> M;
// 수열 출력
perm(N, M, 0);
return 0;
}
Colored by Color Scripter
|