https://www.acmicpc.net/problem/1934
1934번: 최소공배수
두 자연수 A와 B에 대해서, A의 배수이면서 B의 배수인 자연수를 A와 B의 공배수라고 한다. 이런 공배수 중에서 가장 작은 수를 최소공배수라고 한다. 예를 들어, 6과 15의 공배수는 30, 60, 90등이 있
www.acmicpc.net
코드 :
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 {
StringBuilder sb = new StringBuilder();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
int T = Integer.parseInt(br.readLine());
for (int i = 0; i < T; i++) {
st = new StringTokenizer(br.readLine());
int num1 = Integer.parseInt(st.nextToken());
int num2 = Integer.parseInt(st.nextToken());
int lcm = num1 * num2 / GCD(num1, num2);
sb.append(lcm + "\n");
}
System.out.println(sb);
}
public static int GCD(int num1, int num2){
int r = num1 % num2;
if(r == 0){
return num2;
}
return GCD(num2, r);
}
}
'알고리즘 > 백준' 카테고리의 다른 글
백준 1676번 - 팩토리얼 0의 개수 (Java 8) (0) | 2022.03.15 |
---|---|
백준 6588번 - 골드바흐의 추측 (Java 8) (0) | 2022.03.15 |
백준 2608번 - 최대공약수와 최소공배수 (Java 8) (0) | 2022.03.15 |
백준 11656번 - 접미사 배열 (Java 8) (0) | 2022.03.14 |
백준 10824번 - 네 수 (Java 8) (0) | 2022.03.14 |
댓글