까먹기 싫어서 쓰는 순열, 조합
어려워서 외웁니다.
import java.io.*;
import java.util.*;
public class Main {
static int n, r;
static boolean[] visit;
static int[] operations, per_array, com_array;
public static void main(String[] args) throws IOException {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
n = Integer.parseInt(bf.readLine());
r = Integer.parseInt(bf.readLine());
operations = new int[] {1, 2, 3, 4, 5, 6};
visit = new boolean[n];
per_array = new int[r];
com_array = new int[r];
System.out.println("---------순열----------");
per(0);
System.out.println("---------조합----------");
com(0, 0);
System.out.println("---------부분집합----------");
subset();
}
public static void per(int depth) {
if (r == depth) {
System.out.println(java.util.Arrays.toString(per_array));
return;
}
for (int i = 0; i < n; i++) {
if (!visit[i]) {
visit[i] = true;
per_array[depth] = operations[i];
per(depth + 1);
visit[i] = false;
}
}
}
public static void com(int start, int depth) {
if (r==depth) {
System.out.println(java.util.Arrays.toString(com_array));
return;
}
for(int i=start; i < n; i++) {
com_array[depth] = operations[i];
com(i+1,depth+1);
}
}
public static void subset() {
for(int i=0; i<(1<<n); i++) {
Set<Integer> set = new HashSet<>();
for(int j=0; j<n; j++) {
if((i &(1<<j)) != 0) {
set.add(operations[j]);
}
}
System.out.println(set);
}
}
}
주인장님 다음글은 언제 올려주시나요.. 너무 기다려지고 기대돼서 잠이 안와요..