문제

자연수 N과 R이 주어지면 서로 다른 N개의 자연수 중 R개를 뽑아 일렬로 나열하는 프로그램을 작성하세요.

입력설명

첫 번째 줄에 자연수 N(1<=N<=15)과 R(0<=R<=15)이 주어진다. 단 (N>=R)
두 번째 줄에 N개의 서로 다른 자연수가 오름차순으로 주어진다.

출력설명

순열의 각 경우를 아래와 같이 오름차순으로 출력한다. 마지막 줄에 총 개수도 출력한다.

입력예제

4 3
1 3 6 7

출력예제

1 3 6
1 3 7
1 6 3
1 6 7
1 7 3
1 7 6
3 1 6
3 1 7
3 6 1
3 6 7
3 7 1
3 7 6
6 1 3
6 1 7
6 3 1
6 3 7
6 7 1
6 7 3
7 1 3
7 1 6
7 3 1
7 3 6
7 6 1
7 6 3
24


#include <stdio.h>
using namespace std;
int n, r, input[20], result[20], check[20], count=0;

int DFS(int L){
	if(L==r){
		for(int j=0; j<L; j++){
			printf("%d ", result[j]);
		}
		count++;
		puts("");
	} else {
		for(int i=1; i<=n; i++){
			if(check[i]==0){
				result[L] = input[i];
				check[i] = 1;
				DFS(L+1);
				check[i] = 0;
			}
		}
	}
}

int main(){
	freopen("input.txt", "rt", stdin);
	scanf("%d %d", &n, &r);
	for(int i=1; i<=n; i++){
		scanf("%d", &input[i]);
	}
	DFS(0);
	printf("%d", count);
	
	return 0;
}

아!!!!!!!!!!!!!!!!!!!!!!!!!

+ Recent posts