#include <iostream>
#define MAX 9
using namespace std;
int n, m;
int arr[MAX];
bool visited[MAX];
void dfs(int num, int depth) {
if (depth == m) {
for (int i = 0; i < m; i++) {
cout << arr[i] << ' ';
}
cout << '\n';
return;
}
for (int i = num; i <= n; i++) {
if (!visited[i]) {
visited[i] = true;
arr[depth] = i;
dfs(i + 1, depth + 1);
visited[i] = false;
}
}
}
int main() {
cin >> n >> m;
dfs(1, 0);
return 0;
}
N과 M(1) 문제와 똑같은 조건이지만 오름차순이라는 차이점이 있다.
오름차순으로 출력하기 위해서 이전에 추가한 숫자보다 크면 된다!
이전에 추가한 숫자 = dfs 호출 전에 추가한 것이므로 for문을 시작할 index를 증가해주는 형식으로 구현했다.