알고리즘 문제

[C/C++ 백준 15657번] N과 M (8) (Silver 3)

새파란 공대생 2020. 9. 5. 21:18

https://www.acmicpc.net/problem/15657

 

15657번: N과 M (8)

N개의 자연수와 자연수 M이 주어졌을 때, 아래 조건을 만족하는 길이가 M인 수열을 모두 구하는 프로그램을 작성하시오. N개의 자연수는 모두 다른 수이다. N개의 자연수 중에서 M개를 고른 수열 �

www.acmicpc.net

#include <cstdio>
#include <algorithm>
using namespace std;
int N, M, array[8], S[8];
void func(int cnt, int start){
	if(cnt==M){
		for(int i=0; i<M; i++)
			printf("%d ",array[i]);
		printf("\n");
	}
	else{
		for(int i=start; i<N; i++){
			array[cnt]=S[i];
			func(cnt+1, i);
		}
	}
}
int main(void){
	scanf("%d %d", &N, &M);
	for(int i=0; i<N; i++)
		scanf("%d", &S[i]);
	sort(S, S+N);
	func(0, 0);
}