백준|1934번|최소공배수

JSK·2022년 7월 31일
0

자바 PS풀이

목록 보기
14/51

문제설명
말 그대로 두 수를 입력받고 두 수의 최소공배수를 출력하는 문제입니다.

작동 순서
1. 두 수를 입력받습니다.
2. 두 수의 최소공배수는 두 수의 곱/두 수의 최대공약수이므로 두 수의 최대공약수를 구해줍니다.
3. 두 수의 곱/두 수의 최대공약수를 출력합니다.

소스코드

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class 백준_1934번_최소공배수 {
    static int gcd(int a, int b){
        if(a%b==0) return b;
        else{
            return gcd(b,a%b);
        }
    }
    public static void main(String[] args) throws IOException {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        StringBuilder sb=new StringBuilder();
        int T=Integer.parseInt(br.readLine());
        for(int i=0;i<T;i++){
            String input[]=br.readLine().split(" ");
            int n1=Integer.parseInt(input[0]), n2=Integer.parseInt(input[1]);
            sb.append(n1*n2/gcd(n1,n2)+"\n");
        }
        System.out.print(sb);
    }
}

후기
너무 간단한 문제라서 쉽게 풀 수 있었습니다.

profile
학사지만 AI하고 싶어요...

0개의 댓글