[BOJ] 2609번 최대공약수와 최소공배수 - JAVA

최영환·2022년 10월 4일
0

BaekJoon

목록 보기
23/86
post-thumbnail
## Java 풀이 시 유의사항 ##
클래스명은 Main 으로 작성해야함!

💡 문제

💬 입출력 예시

📌 풀이(소스코드)

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

public class Main {
    public static void main(String[] args) throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine());
        int a = Integer.parseInt(st.nextToken());
        int b = Integer.parseInt(st.nextToken());

        int d = gcd(a, b);
        System.out.println(d + "\n" + a * b / d);
    }

    public static int gcd(int a, int b) {
        while (b != 0) {
            int r = a % b;

            a = b;
            b = r;
        }
        return a;
    }
}

📄 해설

  • 유클리드 호제법 을 반복문으로 구현하여 해결하였음
profile
조금 느릴게요~

0개의 댓글