[ ALGORITHM ]/[ 백 준 ]

[BAEKJOON] 15650번 N과 M (2)

HiStar__ 2020. 6. 14. 18:27

문 제

 

소 스  코 드

#include<iostream>

void Func(int, int, const int&, const int&, int *, bool *);
int main() {
	int N, M;
	while (1) {
		std::cin >> N >> M;

		if (M >= 1 && M <= N) {
			if (N >= M && N <= 8) break;
		}
	}

	int *temp{ new int[M] };
	bool *tf{ new bool[N] };

	for (int i = 0; i < N; ++i) tf[i] = false;
	temp[0] = 0;

	Func(0, 0, N, M, temp, tf);

	delete[] tf;
	delete[] temp;

	return 0;
}

void Func(int seq, int start, const int& n, const int& m, int* temp, bool* tf) {
	if (m == seq) {
		for (int i = 0; i < m; ++i) std::cout << temp[i] << " ";
		std::cout << "\n";
		return;
	}

// N과 M (2) 수정
	for (int i = start; i < n; ++i) {
		if (false == tf[i]) {
			tf[i] = true;

			temp[seq] = i + 1;
			Func(seq + 1, i+1, n, m, temp, tf);

			tf[i] = false;
		}
	}
}

 

풀 이

15649번 N과 M (1) ( 링크 : https://studycl.tistory.com/18 )

주의점 : 중복되는 수열을 여러 번 출력하지 않게 하기.

 

만약, Func 함수내에 for문 동작 시 1 ~ n 까지 자연수를 범위로 동작하는데, 

만약, N이 4, M이 2 일 경우, 
[1] [2] 에서 [1]보다 낮은 숫자가 [2]에서 나오면 중복이 있기 때문에 [1]보다 높은 숫자에 있을 경우로
for문의 i 시작을 하면 중복이 없어진다.

 

결 과 값

 

문 제  출 처

문제 링크 : [ BAEKJOON ] https://www.acmicpc.net/problem/15650

'[ ALGORITHM ] > [ 백 준 ]' 카테고리의 다른 글

[BAEKJOON] 9663번 N-Queen  (0) 2020.06.17
[BAEKJOON] 15652번 N과 M (4)  (0) 2020.06.16
[BAEKJOON] 15651번 N과 M (3)  (0) 2020.06.15
[BAEKJOON] 15649번 N과 M (1)  (0) 2020.06.13
[BAEKJOON] 2798번 블랙잭  (0) 2020.06.12