https://st-lab.tistory.com/154
[백준] 2609번 : 최대공약수와 최소공배수 - JAVA [자바]
www.acmicpc.net/problem/2609 2609번: 최대공약수와 최소공배수 첫째 줄에는 입력으로 주어진 두 수의 최대공약수를, 둘째 줄에는 입력으로 주어진 두 수의 최소 공배수를 출력한다. www.acmicpc.net 문제 알
st-lab.tistory.com
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
static boolean arr[];
public static void main(String[] args) throws NumberFormatException, IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine(), " ");
int n1 = Integer.parseInt(st.nextToken());
int n2 = Integer.parseInt(st.nextToken());
StringBuilder sb = new StringBuilder();
sb.append(gcd(n1, n2)).append('\n').append(lcm(n1, n2));
System.out.println(sb);
}
static int gcd(int a, int b) {
if(b == 0) {
return a;
}
return gcd(b, a%b);
}
static int lcm(int a, int b) {
return a * b / gcd(a,b);
}
}
'백준' 카테고리의 다른 글
2438 별찍기 (0) | 2021.10.12 |
---|---|
1918 후위 표기식 (0) | 2021.09.30 |
1978 소수 찾기 (0) | 2021.09.18 |
9663 N-Queen (0) | 2021.09.17 |
9095 1,2,3 더하기 (0) | 2021.09.15 |