3 분 소요

🗒️ 15654번) N과 M (5)

#include <iostream>
#include <algorithm>
using namespace std;

int arr[8];
int isused[8];
int sel[8];
int N, M;

void Sequence(int count)
{
	if (count == M)
	{
		for (int i = 0; i < M; i++)
			cout << sel[i] << ' ';
		cout << '\n';
		return;
	}

	for (int i = 0; i < N; i++)
	{
		if (isused[i])
			continue;

		sel[count] = arr[i];
		isused[i] = true;
		Sequence(count + 1);
		isused[i] = false;
	}
}

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);

	cin >> N >> M;
	for (int i = 0; i < N; i++)
		cin >> arr[i];

	sort(arr, arr + N);
	Sequence(0);

	return 0;
}

카테고리:

업데이트:

댓글남기기