반응형
package N과M;
import java.util.*;
public class N과M6 {
public static boolean visit[];
public static int arr[];
public static int num[];
public static int N,M;
public static void main(String[] args) {
Scanner sc =new Scanner(System.in);
N = sc.nextInt();
M = sc.nextInt();
visit = new boolean[N];
arr = new int[M];
num = new int[N];
for (int i = 0; i < N; i++) {
num[i] = sc.nextInt();
}
Arrays.sort(num);
permutation(0, 0);
}
public static void permutation(int start , int cnt) {
if(cnt == M) {
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
return;
}
for (int i = start; i < N; i++) {
if(!visit[i]) {
visit[i] =true;
arr[cnt] = num[i];
permutation(i, cnt+1);
visit[i] =false;
}
}
}
}
반응형
'2. 알고리즘사이트 > 1. 백준' 카테고리의 다른 글
N과M(8) -오름차순 중복조합 (StringBuilder)- (0) | 2020.03.01 |
---|---|
N과M(7) -오름차순 중복순열 (StringBuilder)- (0) | 2020.03.01 |
N과M(5) -오름차순 순열- (0) | 2020.03.01 |
N과M(4) -중복조합- (0) | 2020.03.01 |
N과M(3) -중복순열- (0) | 2020.03.01 |