본문 바로가기
PS/백준

백준 - 최소공배수

by 종안이 2023. 11. 10.

 

아까 풀었던 유클리드 호제법을 이용한 문제로 최소공배수를 구해준다. 최대공약수를 알면 최소공배수도 구할 수 있음

 

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Stack;

public class Main {
    public static void main(String[] args) throws IOException {

        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
        int minMul = 0;
        int gcd = 0;
        String s = bf.readLine();
        int number = Integer.parseInt(s);
        for (int i = 0; i < number; i++) {
            String s1 = bf.readLine();
            String[] s2 = s1.split(" ");
            int number1 = Integer.parseInt(s2[0]);
            int number2 = Integer.parseInt(s2[1]);
            gcd = getGCD(number1, number2);
            minMul = (number1 * number2) / gcd;
            System.out.println(minMul);
        }

    }

    static int getGCD(int num1, int num2) {

        if (num1 % num2 == 0) {
            return num2;
        }
        return getGCD(num2, num1 % num2);
    }
}

 

'PS > 백준' 카테고리의 다른 글

백준 - 직사각형 탈출  (1) 2023.11.11
백준 - 주사위 세개  (0) 2023.11.11
백준 - 최대공약수와 최소공배수  (0) 2023.11.10
백준 - 일곱 난쟁이  (0) 2023.11.09
백준 - 피보나치 수 5  (1) 2023.11.08

댓글