https://www.acmicpc.net/problem/2609
1. 유형
수학
2. 풀이
유클리드 호제법으로 최대공약수 구한다.
최대공약수를 구했으면, 최소 공배수도 구하는게 가능
3. 코드
import java.io.*;
import java.util.*;
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=stoi(st.nextToken());
int b=stoi(st.nextToken());
int answer;
if(a>b) { answer = dfs(a, b);}
else answer = dfs(b, a);
int answer2 = (b/answer)*(a/answer)*answer;
System.out.println(answer);
System.out.println(answer2);
}
static int dfs(int a, int b){
if(b==0) {
return a;
}
return dfs(b, a%b);
}
static int stoi(String s) {
return Integer.valueOf(s);
}
}
//유클리드 호제법
'알고리즘 > 백준' 카테고리의 다른 글
백준 22857 - 가장 긴 짝수 연속한 부분 수열(small) (0) | 2022.04.05 |
---|---|
[백준 18311] 왕복 (0) | 2022.03.10 |
[백준] 1446 지름길 (0) | 2022.03.06 |
[백준 1251] 단어 나누기 (0) | 2022.03.03 |
[백준 9996] 한국이 그리울 땐 서버에 접속하지 (0) | 2022.03.03 |