ABOUT ME

Today
Yesterday
Total
  • [Algorithm] 조합
    Algorithm 2023. 4. 10. 14:01
    조합

     

    조합이란 n개의 원소를 갖는 집합에서 r개의 원소를 선택하는 것 혹은 선택의 결과로 정의된다.

    어떤 순서로 원소를 선택했는지는 중요하지 않기에 순열과는 다른 개념이다.

     

    구현
    package Algorithm;
    
    import java.util.Arrays;
    
    public class 조합 {
        static int[] arr = {1,2,3,4};
        static int[] sel = new int[2];
        public static void main(String[] args) {
            combination(0,0);
        }
    
        private static void combination(int idx, int s_idx){
            if(s_idx == sel.length){
                System.out.println(Arrays.toString(sel));
                return;
            }
            for(int i=idx; i<arr.length; i++){
                sel[s_idx] = arr[i];
                combination(i+1, s_idx+1);
            }
        }
    }
    결과

     

    [1, 2]
    [1, 3]
    [1, 4]
    [2, 3]
    [2, 4]
    [3, 4]

    'Algorithm' 카테고리의 다른 글

    [Algorithm] BFS  (0) 2023.04.25
    [Algorithm] 부분집합  (0) 2023.04.10
    [Algorithm] 중복 조합  (0) 2023.04.10
    [Algorithm] 중복순열  (0) 2023.04.10
    [Algorithm] 순열  (0) 2023.04.10
Designed by Tistory.