조합 (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));
}
}
'Study for Backend > Mathmatics Basic' 카테고리의 다른 글
[기초 수학] 지수와 로그 (0) | 2024.03.13 |
---|---|
[기초 수학] 점화식과 재귀함수 (0) | 2024.03.12 |
[기초 수학] 약수 , 최대 공약수, 최소 공배수 (0) | 2024.03.11 |
[기초 수학] 경우의 수, 합의 법칙, 곱의 법칙 (0) | 2024.03.11 |
[기초 수학] 집합 (0) | 2024.03.11 |