BOJ_S3_15654
π [S3_15654] Nκ³ΌM(5)
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;
public class Main {
static StringTokenizer st;
static StringBuilder sb;
static int N,M;
static int[] arr;
static boolean[] v;
static int[] res;
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
sb = new StringBuilder();
st = new StringTokenizer(br.readLine(), " ");
N = Integer.parseInt(st.nextToken());
M = Integer.parseInt(st.nextToken());
arr = new int[N];
res= new int[M];
v = new boolean[N];
st = new StringTokenizer(br.readLine(), " ");
for(int i=0; i<N; i++) {
arr[i] = Integer.parseInt(st.nextToken());
}
Arrays.sort(arr);
perm(0);
System.out.println(sb);
}
static void perm(int idx) {
if(idx == M) {
for(int i=0; i<M; i++) {
sb.append(res[i]).append(" ");
}
sb.append("\n");
return;
}
for(int i=0; i<N; i++) {
if(v[i]) {
continue;
}
else {
v[i] =true;
res[idx] = arr[i];
perm(idx+1);
v[i] = false;
}
}
}
}
π€ λμ μκ°
μ΄ λ¬Έμ λ μμκ° μκ΄ μλ λ°°μ΄μ λ½λ λ¬Έμ μ΄λ€. nPr , μμ΄λ¬Έμ μ΄λ€.
λμ΄νλ κ°μ΄ 1,2,3,4 μμ΄ μλ μ§μ μ
λ ₯ν΄μ κ·Έ κ°λ€μ μΆλ ₯ν΄μ€λ€.
κ·Έλμ κ²°κ³Όλ₯Ό μΆλ ₯νλ λ°°μ΄μ arr μΌλ ₯λ°μ κ°λ€μ λ£μ΄μ€λ€.
μμ΄μ λν μμΈν μ€λͺ
μ μ¬κΈ°μ μλ€.