Jun's Development Journey
[완전탐색] 조합 본문
코드
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
|
public class Main {
static int[] comb;//조합 담을 배열
public static void combination(int n, int r, int[] arr) {
comb = new int[r];//조합을 이룰 항목수로 임시 배열 크기 지정
dfs(n,r,0,0,arr);
}
public static void dfs(int n, int r, int st, int depth, int[] arr) {
if(depth == r) {
for(int i=0;i<r;i++)
System.out.print(comb[i]+" ");
System.out.println();
}
else {
for(int i=st;i<n;i++) {
comb[depth] = arr[i];
dfs(n,r,i+1,depth+1,arr);
}
}
}
public static void main(String[] args) {
int[] arr = {1,2,3,4,5};
combination(5,3,arr);//5C3 예시
}
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
|
'Other_Codes' 카테고리의 다른 글
[Permutation] nPn 구현 (0) | 2021.03.22 |
---|---|
[완전탐색] 순열 구하기 (조합 이용 방법, 추가 공간 사용 방법) (0) | 2019.09.27 |
[문자열 처리] 불길한 수열 (2) | 2019.09.20 |
[배열] 밀폐 용기 정리 (0) | 2019.07.16 |