백준 15651번 문제 - N과 M(3)

조하은·2023년 3월 6일
0

BOJ) 백준 15651번 문제 - N과 M(3)

문제: N과 M (3)

난이도 : ★★☆☆☆
학습일 : 2023-03-07

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include<stdio.h>
#include <cmath>
#include <iostream>
using namespace std;

static int N = 0;
static int M = 0;
int* selected;
string sb;

void input() {
	//N과 M 입력받기
	cin >> N >> M;

	//printf("N: %d, M: %d\n", N, M);

	//selected 배열 잡기
	selected = new int[M + 1];
}

void rec_func(int k) {
	if (k == M + 1) {	//다 고른 경우
		//selected[1..M] 배열이 새롭게 탐색된 결과

		//검색된 결과 출력하기
		for (int i = 1; i <= M; i++) {
			sb += to_string(selected[i]) + " ";
			//cout << selected[i] << " ";
		}
		sb += "\n";
	}
	else {
		for (int cand = 1; cand <= N; cand++) {
			selected[k] = cand;

			//k + 1번부터 M까지 모두 탐색해야 함
			rec_func(k + 1);
			selected[k] = 0;
		}
	}
}

int main() {
	
	input();

	//첫 번째 원소부터 M번째 원소까지 보면서 조건에 맞는 모든 방법 찾기
	rec_func(1);

	cout << sb;
	
	return 0;
}

0개의 댓글