법칙

황상익·2023년 10월 25일
0

자료구조 정리

목록 보기
9/13

<기본 code>

public class Chap_02 {
    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;

        // 기본 풀이
        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 + nA - nAandB));


        // HashSet 이용
        HashSet<ArrayList> allCase = new HashSet<>(); // 주사위 눈금이 모든 데이터가 저장 될 수 있도록
        for (int item1 : dice1) {
            for (int item2 :
                    dice2) {
                if ((item1 + item2) % 3 == 0 || (item1 + item2) % 12 == 0) {
                    ArrayList list = new ArrayList(Arrays.asList(item1, item2));
                    // 주사위 2개 눈금을 리스트로 만든 다음 -> hashset에 넣어주면 모든 주사위 눈금 정보를 기록
                    // hashset을 이용해서 add -> 중복된 수는 모두 빠진다.
                    allCase.addAll(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));

    }
}

<연습문제>

package practice;

// Practice
// 약수 구하기, 두 수의 최대공약수와 최소공배수 구하기
// 활용) 1~10의 수 중 A의 약수 또는 B의 약수인 경우의 수
// 활용) 1~10의 수 중 A의 약수이면서 B의 약수인 경우의 수
// 10: 1, 2, 5, 10
// 6: 1, 2, 3, 6
import java.util.ArrayList;

public class Practice_02 {
    //  약수
    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);

        return result;
    }

    //  최대 공약수
    // 최대 공약수를 구해서 int 값을 구한 후 두개의 수를 받아 오도록
//  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;
                        // itemA == itemB 이고, itemA가 gcd 값보다 큰 값이 나오면, 계속해서 큰 공약수로 변경
                    }
                }
            }
        }
        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;

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

아직 최소공배수 최대공약수 부분이 익숙하지는 않은 것 같다.

profile
개발자를 향해 가는 중입니다~! 항상 겸손

0개의 댓글