수학에서 경우의 수란 주어진 집합에서 일부 요소를 선택하여 만들어지는 부분집합의 개수를 의미합니다.
경우의 수는 어떤 상황에서 가능한 모든 조합의 개수를 의미합니다. 예를 들어, 1부터 5까지의 숫자 중에서 3개의 숫자를 선택하여 만들 수 있는 모든 조합의 개수가 경우의 수가 됩니다.
자바에서 경우의 수를 계산하기 위해서는 주로 조합(Combination)을 사용합니다. 조합은 순서가 중요하지 않고, 중복된 요소가 없는 조합을 의미합니다. 경우의 수를 계산할 때는 주어진 집합의 크기와 선택할 요소의 개수에 따라서 다양한 방법을 사용할 수 있습니다.
사건 A 또는 B가 일어날 경우의 수
사건 A와 사건 B의 합의 법칙 :

사건 A와 사건 B의 곱의 법칙 :

자바에서 경우의 수를 계산하기 위해서는 주로 재귀 함수나 반복문을 활용합니다. 이를 통해 가능한 모든 조합을 생성하고 개수를 세는 방식으로 경우의 수를 구할 수 있습니다. 또한, 조합을 구현한 외부 라이브러리를 사용할 수도 있습니다.
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
public class Main {
public static void main(String[] args) {
// 1. 합의 법칙
System.out.println("== 합의 법칙 ==");
// 두 개의 주사를 던졌을 때 합이 3 또는 4의 배수일 경우의 수
int[] dice1 = {1, 2, 3, 4, 5, 6};
int[] dice2 = {1, 2, 3, 4, 5, 6};
int nA = 0;
int nB = 0;
int nAandB = 0;
// 기본 풀이
for (int item1 : dice1) {
for (int item2 : dice2) {
if ((item1 + item2) % 3 == 0) {
nA += 1;
}
if ((item1 + item2) % 4 == 0) {
nB += 1;
}
if ((item1 + item2) % 12 == 0) {
nAandB += 1;
}
}
}
System.out.println("결과: " + (nA + nB - nAandB));
// HashSet 이용
HashSet<ArrayList> allCase = new HashSet<>();
for (int item1 : dice1) {
for (int item2 : dice2) {
if ((item1 + item2) % 3 == 0 || (item1 + item2) % 4 == 0) {
ArrayList list = new ArrayList(Arrays.asList(item1, item2));
allCase.add(list);
}
}
}
System.out.println("결과: " + allCase.size());
// 2. 곱의 법칙
System.out.println("== 곱의 법칙 ==");
// 두 개의 주사위 a, b를 던졌을 때 a는 3의 배수, b는 4의 배수인 경우의 수
nA = 0;
nB = 0;
for (int item1 : dice1) {
if (item1 % 3 == 0) {
nA++;
}
}
for (int item1 : dice2) {
if (item1 % 4 == 0) {
nB++;
}
}
System.out.println("결과: " + nA * nB);
}
}
// 약수 구하기, 두 수의 최대공약수와 최소공배수 구하기
// 활용) 1~10의 수 중 A의 약수 또는 B의 약수인 경우의 수
// 활용) 1~10의 수 중 A의 약수이면서 B의 약수인 경우의 수
import java.util.ArrayList;
public class Practice {
// 약수
public ArrayList getDivisor(int num) {
ArrayList result = new ArrayList();
for (int i = 1; i <= (int) num / 2; i++) {
if (num % i == 0) {
result.add(i);
}
}
result.add(num);
return result;
}
// 최대 공약수
// GCD: the Greatest Common Denominator
public int getGCD(int numA, int numB) {
int gcd = -1;
ArrayList divisorA = this.getDivisor(numA);
ArrayList divisorB = this.getDivisor(numB);
for (int itemA : (ArrayList<Integer>) divisorA) {
for (int itemB : (ArrayList<Integer>) divisorB) {
if (itemA == itemB) {
if (itemA > gcd) {
gcd = itemA;
}
}
}
}
return gcd;
}
// 최소 공배수
// LCM: the Lowest Common Multiple
public int getLCM(int numA, int numB) {
int lcm = -1;
int gcd = this.getGCD(numA, numB);
if (gcd != -1) {
lcm = numA * numB / gcd;
}
return lcm;
}
public static void main(String[] args) {
int number1 = 10;
int number2 = 6;
Practice p = new Practice();
ArrayList l1 = p.getDivisor(number1); // 10: 1, 2, 5, 10
ArrayList l2 = p.getDivisor(number2); // 6: 1, 2, 3, 6
System.out.println("l1 = " + l1);
System.out.println("l2 = " + l2);
System.out.println("최대 공약수: " + p.getGCD(number1, number2));
System.out.println("최소 공배수: " + p.getLCM(number1, number2));
}
}