알고리즘/백준
[백준] 2609번 - 최대공약수와 최소공배수
땀두
2022. 3. 19. 23:19

import java.util.Scanner;
public class p2609 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
int c = a * b;
while (b != 0) {
int r = a % b;
a = b;
b = r;
}
System.out.println(a);
System.out.println(c/a);
}
}
모듈러 연산을 통해서 간단하게 최대공약수를 구한 뒤 a, b의 곱에서 최소공배수를 나누어주어 최소공배수를 구한다.