Jun's Development Journey

[완전탐색] 조합 본문

Other_Codes

[완전탐색] 조합

J_Jayce 2019. 9. 26. 23:15

코드

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
import java.util.*;
 
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