Hyunebee

조합 본문

zerebase/기초수학

조합

Hyunebee 2022. 4. 2. 01:53

조합

서로다른 n개중에서 r개를 선택하는 방법 (순서x, 중복x)

 

https://velog.io/@jelkov/%EC%88%9C%EC%97%B4-%EC%A1%B0%ED%95%A9-%EC%B6%94%EA%B0%80%EC%A0%81%EC%9D%B8-%EB%82%B4%EC%9A%A9

 

중복조합

서로다른 n개중에서 r개를 선택하는 방법 (순서x, 중복o)

ex) 4가지 서로다른 숫자중 3자리숫자를 만들수 있는 경우의 수(순서x, 중복 x)

 

public class Practice {
    void combination(int[] arr, boolean[] visited, int depth, int n, int r) {

        if(r == 0){ // 더이상 채울 자리가 없으면
            for(int i = 0; i < n; i++){
                if(visited[i]){
                    System.out.print(arr[i] + " ");
                }
            }
            System.out.println();
            return;
        }

        if(depth == n){ // 더이상 탐색할 곳이 없다면
            return;
        }


        visited[depth] = true;
        combination(arr,visited,depth+1, n, r-1);
        visited[depth] = false;
        combination(arr,visited,depth+1, n, r);

    }

    public static void main(String[] args) {
//      Test code
        int[] arr = {1, 2, 3, 4};
        boolean[] visited = {false, false, false, false};

        Practice p = new Practice();
        p.combination(arr, visited, 0, 4, 3);
    }
}

 

 

 

 

 

 

 

 

'zerebase > 기초수학' 카테고리의 다른 글

하노이탑  (0) 2022.04.09
지수와 로그  (0) 2022.04.02
순열  (0) 2022.03.29
경우의 수  (0) 2022.03.28