Post

조합 (Combination)

1. 조합

  • 서로 다른 n개 중에서 r개를 선택하는 경우의 수 (순서 X, 중복 X)
    • 예시) 서로 다른 4명 중 주번 2병 뽑기

Untitled

2. 중복조합

  • 서로 다른 n개 중에서 r개를 선택하는 경우의 수 (순서 X, 중복 O)
    • 예시) 후보 2명, 유권자 3명일 때 무기명 투표 방법

Untitled 1

(실습 : 조합)

Untitled 2

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
// Practice
// 1, 2, 3, 4 를 이용하여 세자리 자연수를 만드는 방법 (순서 X, 중복 x)의 각 결과를 출력하시오

import java.util.Arrays;

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);
    }
}
This post is licensed under CC BY 4.0 by the author.