백준 15652 c++

magicdrill·2024년 4월 11일

백준 문제풀이

목록 보기
288/673

백준 15652 c++

#include <iostream>
//1부터 N까지 M개 고름, 중복 가능, 비내림차순...
//내림차순인데 자기보다 작은 수는 포함 안함..
using namespace std;

int N, M;
int* arr;
bool* visited;

int input(int lower, int upper)
{
	//cout << "input()" << endl;
	int A;

	while (1)
	{
		cin >> A;
		if (A >= lower && A <= upper)
		{
			break;
		}
		else
		{
			;
		}
	}

	return A;
}

void dfs(int num, int cnt)
{
	//cout << "dfs()\n";
	int i;

	if (cnt == M)//목표 M까지 도달한경우
	{
		for (i = 0; i < M; i++)
		{
			cout << arr[i] << " ";
		}
		cout << "\n";
		return;
	}
	else//목표 M까지 도달하지 못 한 경우
	{
		for (i = num; i <= N; i++)
		{
			//중복가능, 방문기록이 있어도 가능
			visited[i] = true;
			arr[cnt] = i;
			dfs(i, cnt + 1);
			visited[i] = false;
		}
	}
}

int main(void)
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);

	N = input(1, 8);
	M = input(1, N);
	arr = new int[N + 1] {0};
	visited = new bool[N + 1] {0};
	dfs(1, 0);

	delete[] arr;
	delete[] visited;

	return 0;
}

0개의 댓글