1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 | package basic; import java.util.ArrayList; import java.util.Arrays; public class NumberOfCases { static char[] arr = { 'a', 'b', 'c', 'd' }; static int r = 2; public static void main(String[] args) { set = new char[r]; // System.out.println("==조합=="); // nCr(0,0); //comb(r,arr.length); // System.out.println("==중복조합=="); // nHr(0,0); //System.out.println("==중복순열=="); //nPir(0); setList = new ArrayList<>(); //subset(0,0); visit = new boolean[arr.length]; nPr(0); } static char[] set; public static void nCr(int len, int k) { // 조합 if (len == r) { System.out.println(Arrays.toString(set)); return; } for (int i = k; i < arr.length; i++) { set[len] = arr[i]; nCr(len + 1, i + 1); } } public static void comb(int depth, int idx) { if (depth==0) { System.out.println(Arrays.toString(set)); } else if (idx < depth) { return; } else { //System.out.println(depth + " " + idx ); set[depth-1] = arr[idx-1]; comb(depth - 1, idx - 1); comb(depth, idx-1); } } public static void nHr(int len, int k) { // 중복조합 if (len == r) { System.out.println(Arrays.toString(set)); return; } for (int i = k; i < arr.length; i++) { set[len] = arr[i]; nHr(len + 1, i); } } static boolean[] visit; public static void nPr(int len) {// 순열 if(len==r) { System.out.println(Arrays.toString(set)); return; } for(int i=0;i<arr.length;i++) { if(!visit[i]) { set[len]=arr[i]; visit[i]=true; nPr(len+1); visit[i]=false; } } } public static void nPir(int len) {// 중복순열 if(len==r) { System.out.println(Arrays.toString(set)); return; } for(int i=0;i<arr.length;i++) { set[len]=arr[i]; nPir(len+1); } } static ArrayList<Character> setList; public static void subset(int len, int k) {// 부분집합 System.out.println(setList); if(len==arr.length) { return; } for(int i=k;i<arr.length;i++) { setList.add(arr[i]); subset(len+1,i+1); setList.remove(setList.size()-1); } } } | cs |
'Computer Science > Algorithms' 카테고리의 다른 글
[java] Dijkstra 다익스트라 알고리즘 (0) | 2019.02.19 |
---|---|
Kruskal 크루스칼 알고리즘 (0) | 2019.02.18 |
DisjointSets (0) | 2019.02.18 |
[java] Insertion Sort 삽입정렬 (0) | 2019.01.28 |
Permutation 순열 (0) | 2019.01.17 |