CS/알고리즘_문제풀이(자바)

최소공배수

Jedy_Kim 2021. 9. 17. 14:42
728x90

https://www.acmicpc.net/problem/1934

 

1934번: 최소공배수

두 자연수 A와 B에 대해서, A의 배수이면서 B의 배수인 자연수를 A와 B의 공배수라고 한다. 이런 공배수 중에서 가장 작은 수를 최소공배수라고 한다. 예를 들어, 6과 15의 공배수는 30, 60, 90등이 있

www.acmicpc.net

// 코드

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import java.util.*;
import java.io.*;
 
public class Main{ 
  public static int GCD(int A, int B) {
    while(true) {
      int r = A % B;
      if(r == 0) {
        return B;
      }
      A = B;
      B = r;
    }
  }
  
  public static void main(String[] args) throws Exception {
 
    // Please Enter Your Code Here
    BufferedReader br  = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer(br.readLine());
    
    int T = Integer.parseInt(st.nextToken());
    for(int i=0; i<T; i++) {
      
      st = new StringTokenizer(br.readLine());
      int B = Integer.parseInt(st.nextToken());
      int A = Integer.parseInt(st.nextToken());
      
      int gcd = GCD(A, B);
      int lcm = (B/gcd)*(A/gcd)*gcd;
      System.out.println(lcm);
    }
    
  }
}
cs

 

반응형

'CS > 알고리즘_문제풀이(자바)' 카테고리의 다른 글

1로 만들기  (0) 2021.09.20
GCD 합  (0) 2021.09.17
DFS와 BFS  (0) 2021.09.15
ABCDE  (0) 2021.09.15
N과 M (2)  (0) 2021.09.15