2023년 1월 1일
08:00 AM
Buffering ...

최근 글 👑

사전캠프 3일차 알고리즘 코드 카타 _ 프로그래머스 최대공약수와 최소공배수

2024. 7. 3. 17:50ㆍ내배캠_Java 6기/알고리즘 코드카타

https://school.programmers.co.kr/learn/courses/30/lessons/12940

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

 

내 코드 👇

import java.util.Arrays;
import java.util.Scanner;

public class Solution{
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);

        int a= sc.nextInt();
        int b= sc.nextInt();
        
        Solution sol=new Solution();

        int[] answer = sol.solution(a,b);

        System.out.println(Arrays.toString(answer));
    }

    public static int[] solution(int n, int m){
        int[] answer = new int[2];
        answer [0] = gcd(n, m);
        answer[1] = lcm(n, m);

        return answer;
    }


    // 최소공배수
    public static int lcm(int a, int b) {
        return (a * b) / gcd(a, b);
    }

    // 최대공약수
    public static int gcd(int a,int b) {
        while (b != 0) {
            int temp = a % b;

            a = b;

            b = temp;
        }

        return a;
    }
}
728x90