Algorithm/Algorithm
[재귀] N과 M(2)
lee308812
2019. 10. 28. 05:06
예제 입력 1
3 1
예제 출력 1
1
2
3
예제 입력 2
4 2
예제 출력 2
1 2
1 3
1 4
2 3
2 4
3 4
예제 입력 3
4 4
예제 출력 3
1 2 3 4
#include <stdio.h>
constexpr int MAX_SIZE = 8;
bool checked[MAX_SIZE];
int N, M;
void go(int now, int cnt)
{
if (cnt == M)
{
for (int i = 0; i < N; i++)
{
if (checked[i]) printf("%d ", i + 1);
}
printf("\n");
return;
}
for (int i = now; i < N; i++)
{
if (checked[i]) continue;
checked[i] = true;
go(i + 1, cnt + 1);
checked[i] = false;
}
}
int main(void)
{
scanf("%d %d", &N, &M);
go(0, 0);
return 0;
}