본문 바로가기
algorithm/ACMICPC

N과 M (2) - 15650

by 에어컨조아 2019. 11. 3.

문제 : https://www.acmicpc.net/problem/15650

 

15650번: N과 M (2)

한 줄에 하나씩 문제의 조건을 만족하는 수열을 출력한다. 중복되는 수열을 여러 번 출력하면 안되며, 각 수열은 공백으로 구분해서 출력해야 한다. 수열은 사전 순으로 증가하는 순서로 출력해야 한다.

www.acmicpc.net

 

코드 : 

#include <iostream>

using namespace std;

int arr[10];
bool check[10];

void solution(int start, int index, int n, int m)
{
	//탈출조건
	if (index == m)
	{
		for (int i = 0; i < m; i++)
		{
			cout << arr[i] << " ";
		}
		cout << "\n";
		return;
	}

	for (int i = start; i <= n; i++)
	{
		if (check[i]) continue;
		arr[index] = i;
		check[i] = true;
		solution(i + 1, index + 1, n, m);
		check[i] = false;
	}
}

int main()
{
	int n, m;
	cin >> n >> m;

	solution(1, 0, n, m);

	return 0;
}

 

1. 재귀함수를 구현할때 매개변수 각각 따로따로 생각해서 디버깅해보니 조금 더 편했음.

2. N과 M (1) 15649 문제와 거의 비슷함. 

'algorithm > ACMICPC' 카테고리의 다른 글

N과 M (6) - 15655  (0) 2019.11.14
N과 M (5) - 15654  (0) 2019.11.14
N과 M (4) - 15652  (0) 2019.11.05
N과 M (3) - 15651  (0) 2019.11.05
N과 M (1) - 15649  (0) 2019.11.03

댓글