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;
}

 

'Algorithm > Algorithm' 카테고리의 다른 글

[재귀] N과 M(1)  (0) 2019.10.28
순열 활용하기  (0) 2019.09.13
조합 활용하기  (0) 2019.09.13
파스칼의 삼각형  (0) 2018.10.01
[그래프] 인접 리스트  (0) 2018.08.21