본문 바로가기
프로그래머 ,백준, 유튜브, 문제

최대공약수와 최소공배수_프로그래머스

by 리승우 2022. 9. 27.

[필수개념]

> 유클리드 호제법을 알고 있어야함

class Solution {
    public int[] solution(int n, int m) {
        int[] answer = new int[2];
        int minnum = Math.min(n,m);
        int maxnum = Math.max(n,m);

        answer[0] = gcd(maxnum,minnum);
        answer[1] = maxnum*minnum/answer[0];

        return answer;
    }

        public int gcd(int a, int b){
        if(a%b ==0){
            return b;
        }
        return gcd(b, a%b);
        }
}


    public class Hello {
        public static void main(String[] args) {
            Solution test = new Solution();
            int o = 2;
            int p = 5;
            System.out.println(test.solution(o,p));
        }
    }

댓글