public class Main {
public static void main(String[] args) {
int n = 10, r = 5;
int[] ret = new int[r];
System.out.println(n + "C" + r);
combi(ret, n, r, 0, 0);
System.out.println("\n"+n + "P" + r);
boolean[] visited = new boolean[n];
ret = new int[r];
perm(ret, visited, 0, n, r);
}
//nCr
static void combi(int[] ret, int n, int r, int index, int target) {
if (r == 0) {
for (int i : ret) {
System.out.print(i + " ");
}
System.out.println();
return;
}
if (target == n) return;
ret[index] = target;
combi(ret, n, r - 1, index + 1, target + 1); //뽑는 경우
combi(ret, n, r, index, target + 1); //안뽑는 경우
}
/* 중복 조합
combi(ret, n, r - 1, index + 1, target + 1);
-> combi(ret, n, r - 1, index + 1, target);
* */
//nPr
static void perm(int[] ret, boolean[] visited, int depth, int n, int r) {
if (depth == r) {
for (int i : ret) {
System.out.print(i + " ");
}
System.out.println();
return;
}
for (int i = 0; i < n; i++) {
if (visited[i]) continue;
visited[i] = true;
ret[depth] = i;
perm(ret, visited, depth + 1, n, r);
visited[i] = false;
}
}
/*중복 순열
visited 부분 제거
*/
}
컴/알고리즘