PS/inflearn java coding
순열 출력
easy16
2022. 4. 12. 23:58
1.10이하의 N개 자연수가 주어지면 이중 M개를 뽑아 일렬로 나열하는 방법모두 출력
class Main {
static int m, n;
public void DFS( int level , int [] perm, int[] ch,int []arr) {
if( level == m ) {
for ( int x : perm)
System.out.print(x+" ");
System.out.println();
return;
}else {
for ( int i = 0 ; i < n ; i ++) {
//중복 체크
if( ch[i] == 0 ) {
ch[i]=1;
perm[level] = arr[i];//D1,D2,D3 3, 6 ,9
DFS(level+1, perm, ch, arr);
ch[i]=0;
}
}
}
}
public static void main(String args[]) throws Exception {
Main M = new Main();
Scanner in = new Scanner(System.in);
n = in.nextInt();
m = in.nextInt();
int arr[] = new int[n];
int ch[] = new int[n];//중복확인용
int perm[] = new int[m];
for ( int i = 0 ; i < n ; i++)
arr[i] = in.nextInt();
//from level 0
M.DFS(0, perm, ch ,arr);
}
}
ex)
input
3 2
3 6 9
output
3 6
3 9
6 3
6 9
9 3
9 6