TIP !


유클리드 호제법

GCD(A, B) = GCD (B, A % B)

if A%B = 0 -> GCD = B

       else GCD (B, A%B)

 

 

import java.util.Scanner;

public class Main {

    public static int greatest_common_factor(int a, int b){
        int A = a;
        int B = b;
        int r = a % b;

        if( r == 0)
            return B;

        int res = greatest_common_factor(B , r);

        return res;
    }

    public static int least_common_multiple(int ab, int greatest_common_factor){
        return ab / greatest_common_factor;
    }

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

        int a = sc.nextInt();
        int b = sc.nextInt();
        int greatest_common_factor = greatest_common_factor(a, b);
        int least_common_multiple = least_common_multiple(a*b,greatest_common_factor);
        System.out.println(greatest_common_factor);
        System.out.println(least_common_multiple);
    }
}

'AREA(지속적인 일상) > 02_백준' 카테고리의 다른 글

[백준] - 2164 - 카드2  (0) 2021.09.07
[백준] - 10773 - 제로  (0) 2021.09.07
[백준] - 참고. 자릿수 구하기  (0) 2021.08.30
[백준] - 1546 - 평균  (0) 2021.08.26
[백준] - 3052 - 나머지  (0) 2021.08.26