본문 바로가기
algorithm/ACMICPC

N과 M (1) - 15649

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

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

 

15649번: N과 M (1)

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

www.acmicpc.net

 

코드 : 

#include <iostream>
using namespace std;

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

void solution(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 = 0; i < n; i++)
	{
		if (check[i]) continue;
		arr[index] = i+1;
		check[i] = true;
		solution(index+1, n, m);
		check[i] = false;
	}
	
}

int main()
{
	int n, m;
	cin >> n >> m;
	solution(0, n, m);
    
	return 0;
}

1. 재귀함수 구현에 대해서 미흡함을 느낌..  

2. 개행 출력을 할때 endl 보단 "\n"을 사용하자! (가끔 이로인해 시간초과가 뜸)

'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 (2) - 15650  (0) 2019.11.03

댓글