[C++] BAEKJOON 15650

HYOoMi·2021년 3월 16일
0

N과 M(2)

코드

#include <iostream>
#define MAX 9
using namespace std;

int N, M;
int arr[MAX];
bool visit[MAX];

void dfs(int first, int cnt) {
	if (cnt == M) {
		for (int i = 0; i < M; i++) {
			cout << arr[i] << ' ';
		}
		cout << '\n';
		return;
	}
	for (int i = first; i <= N; i++){
		if (!visit[i]) {
			visit[i] = true;
			arr[cnt] = i;
			dfs(i + 1, cnt + 1);
			visit[i] = false;
		}
	}
}

int main(void) {
	cin.tie(NULL);  ios::sync_with_stdio(false);
	cin >> N >> M;
	dfs(1, 0);
}

0개의 댓글

관련 채용 정보