경우의 수

sebeen·2025년 2월 13일

기초수학

목록 보기
2/8

기본 코드

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; // 합이 3일 경우의 수
        int nB = 0; // 합이 4일 경우의 수
        int nAandB = 0; // 겹치는거

        // 1-1. 기본 풀이
        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));

        // 1-2. 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 item2 : dice2) {
            if (item2 % 4 == 0) {
                nB++;
            }
        }

        System.out.println("결과: " + (nA * nB));
    }
}

약수구하기, 최대공약수&최소공배수 구하기

// Practice
// 약수 구하기, 두 수의 최대공약수와 최소공배수 구하기

import java.util.ArrayList;

public class Practice1 {

    //  약수
    public ArrayList getDivisor(int num) {
        ArrayList result = new ArrayList();
        for (int i = 1; i <= (int) num / 2; i++) { // 절반만 for문 돌리기
            if (num % i == 0) {
                result.add(i);
            }
        }
        result.add(num); // for문 다 끝난 뒤에 자기자신도 넣어줌

        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) {
            // divisorA를 ArrayList<Integer>로 변환하는 것
            // (divisorA는 일반 ArrayList 타입으로 선언되어 있어서,
            // 타입 안전성을 보장하기 위해 특정 타입으로 변환하는 작업이 필요)
            // 이렇게 하면, 리스트에서 꺼내는 요소가 Integer 타입임을 보장받을 수 있음.
            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) {

//      Test code
        int number1 = 10;
        int number2 = 6;

        Practice1 p = new Practice1();
        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));
    }
}

0개의 댓글