Post

경우의 수

1. 경우의 수

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

2. 합의 법칙

  • 사건 A 또는 사건 B가 일어날 경우의 수
  • n (A U B)

Untitled

  • n(A U B) = n(A) + n(B) - n(A ∩ B)

3. 곱의 법칙

  • 사건 A와 사건 B가 동시에 일어날 경우의 수
  • n (A x B)

Untitled 1

(실습 : 경우의 수)

Untitled 2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// 기초 수학 - 경우의 수

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("결과(HashSet 이용) : " + 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));

    }
}
This post is licensed under CC BY 4.0 by the author.