프로그래머스 피자 나눠 먹기(2) (최대공약수, 최소공배수)

박철현·2023년 5월 24일

프로그래머스

목록 보기
24/80

프로그래머스 - 피자 나눠 먹기(2)

class Solution {
    public int solution(int n) {
       if(n%6 ==0)
           return n/6;
        return LCMCalculator.getLCM(n, 6) / 6;
    }
}

class LCMCalculator {
    // 최대공약수를 구하는 메소드
    public static int getGCD(int a, int b) {
        if (b == 0) {
            return a;
        }
        return getGCD(b, a % b);
    }

    // 최소공배수를 구하는 메소드
    public static int getLCM(int a, int b) {
        int gcd = getGCD(a, b);
        return (a * b) / gcd;
    }
}
  • 최대공약수, 최소공배수 저장해두고 라이브러리로처럼 가져다 쓰자!

  • 다른 사람의 간단 풀이

class Solution {
    public int solution(int n) {
    //1판은 6조각, n명이 동등하게
         for(int i = 1; i<n; i++){
             if((i * 6) % n == 0) return i;
         }
        return n;
    }
}
  • 간단하네..
profile
비슷한 어려움을 겪는 누군가에게 도움이 되길

0개의 댓글