본문 바로가기

Study for Backend/Mathmatics Basic

[기초 수학] 조합

조합 (Combination)

서로 다른 n개 중에서 r개를 뽑아서 선택하는 경우의 수 ( 순서 정렬 및 중복 불가 )

- 예시 ) 서로 다른 4명 중 주번 2명을 뽑는 방법

 

중복 조합 

서로 다른 n개 중에서 r개를 선택하는 경우의 수 ( 순서 정렬은 불가능하나 중복 허용 가능)

- 예시 ) 후보 2명, 유권자 3명일 때 무기명 투표 방법

 

 


// 기초 수학 - 조합

public class mathPractice05 {
    static int getCombination(int n , int r){
        int pResult = 1;
        for (int i = n; i >= n - r + 1; i--) {
            pResult *= i;
        }

        int rResult = 1;
        for (int i = 1; i <= r; i++) {
            rResult *= i;
        }
        return pResult/rResult;
    }
    
    public static void main(String[] args) {
        //조합
        System.out.println("=== 조합 ===");
        //서로 다른 4명 중 주변 2명 뽑는 경우의 수
        int n = 4;
        int r = 2;

        int pResult = 1;
        for (int i = n; i >= n - r + 1; i--) {
            pResult *= i;
        }

        int rResult = 1;
        for (int i = 1; i <= r; i++) {
            rResult *= i;
        }
        System.out.println("조합 결과 : " + pResult/rResult);

        //중복조합
        System.out.println("=== 중복 조합 ===");
        //후보 2명, 유권자 3명일 때 무기명 투표 경우의 수
        n = 2;
        r = 3;
        System.out.println("중복 조합 결과 : " + getCombination(n + r - 1, r));

    }
}