경우의 수

경우의 수

어떤 사건에서 일어날 수 있는 경우의 가짓수
예시 1) 동전을 던지는 사건: 경우의 수 2
예시 2) 주사위를 던지는 사건: 경우의 수 6
사건 A가 일어날 경우의 수: n(A)

합의 법칙

사건 A 또는 사건 B가 일어날 경우의 수
사건 A와 사건 B의 합의 법칙: n(A ∪ B)
예시) 두 개의 주사위를 던졌을 때 합이 3 또는 4의 배수일 경우의 수

  • 사건 A: 합이 3의 배수일 경우
    3: (1, 2), (2, 1)
    6: (1, 5), (2, 4), (3, 3), (4, 2), (5, 1)
    9: (3, 6), (4, 5), (5, 4), (6, 3)
    12: (6, 6)

  • 사건 B: 합이 4의 배수일 경우
    4: (1, 3), (2, 2), (3, 1)
    8: (2, 6), (3, 5), (4, 4), (5, 3), (6, 2)
    12: (6, 6)

n(A ∪ B) = n(A) + n(B) – n(A ∩ B)
→ 12 + 9 – 1 = 20

곱의 법칙

사건 A와 사건 B가 동시에 일어날 경우의 수
사건 A와 사건 B의 곱의 법칙: n(A x B)
예시) 두 개의 주사위 a, b를 던졌을 때 a는 3의 배수, b는 4의 배수인 경우의 수

  • 사건 A: a가 3의 배수일 경우
    2가지: 3, 6

  • 사건 B: b가 4의 배수일 경우
    1가지: 4

n(A x B) = n(A) x n(B)
→ 2 x 1 = 2

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;

public class Practice2 {
    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) {
                    nA += 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) {
                    //Arrays.asList()는 고정된 List다. List 원소를 추가 할 수도, 삭제할 수도 없다.
                    //만약 원소를 추가/삭제를 하고 싶다면 다음과 같이 변환해주면 된다.
                    ArrayList list = new ArrayList(Arrays.asList(item1, item2));
                    //Set인터페이스는 중복을 허용하지 않고, 순서가 유지되지 않는 컬렉션 클래스를 구현하는데 사용되는 인터페이스입니다.
                    // HashSet, TreeSet는 Set인터페이스를 구현하므로, 중복된 값은 저장하지 않고, 순서에 상관없이 저장합니다.
                    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));
    }
}
== 합의 법칙 ==
결과: 20
결과: 20
== 곱의 법칙 ==
결과: 2
//역수 구하기, 두 수의 최대공약수와 최소공배수 구하기
//활용) 1~10의 수 중 A의 약수 또는 B의 약수인 경우의 수
//활용) 1~10의 수 중 A의 약수이면서 B의 약수인 경우의 수

import java.util.ArrayList;

public class Practice2_1 {

    public static void main(String[] args) {
        //Test Code
        int number1 = 10;
        int number2 = 6;

        Practice2_1 p = new Practice2_1();
        ArrayList l1 = p.getDivisor(number1);
        ArrayList l2 = p.getDivisor(number2);
        System.out.println("l1 = " + l1);
        System.out.println("l2 = " + l2);

        System.out.println("최대 공약수: " + p.getGCD(number1, number2));
        System.out.println("최소 공배수: " + p.getLCM(number1, number2));
    }

    //약수: 4: 1,2,4 나눴을때 나머지가 발생하지 않는 수. 6: 1,2,3,6
    public ArrayList getDivisor(int num) {
        ArrayList result = new ArrayList();

        //절반 까지만 돌림. 왜냐하면 6이라고 하면 6의 반은 3인데, 3위의 수 4, 5는 나누어 떨어지지 않기 때문.
        for (int i = 1; i <= (int) num / 2; i++) {
            if (num % i == 0) {
                result.add(i);
            }
        }
        //for문 끝나고 자기자신도 넣어줌.
        result.add(num);
        return result;
    }

    //최대 공약수  4: 1,2,4  | 6: 1,2,3,6 2가 최대공약수
    //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;
    }

    //최소 공배수 4: 4,8,12.... | 6: 6,12,18...   공식 6 * 4 / 2 = 12
    //최소공배수 (LCM): 두 개 이상의 정수의 공통 배수 중에서 가장 작은 정수를 의미합니다.
    //LCM: the Lowest Common Multiple
    public int getLCM(int numA, int numB) {
        int lcm = -1;

        //LCM(a, b) = a * b / GCD(a, b).
        int gcd = this.getGCD(numA, numB);

        if (gcd != -1) {
            lcm = numA * numB / gcd;
        }

        return lcm;
    }
}
l1 = [1, 2, 5, 10]
l2 = [1, 2, 3, 6]
최대 공약수: 2
최소 공배수: 30

출처 : 제로베이스

Leave a comment