티스토리 뷰

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

class Main {
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String[] input = in.readLine().split(" ");
int[] numbers = new int[2];
for(int i=0; i<2; ++i) {
numbers[i] = Integer.parseInt(input[i]);
}
//최대공약수
int a = numbers[0];
int b = numbers[1];
while(b != 0) {
int temp = b;
b = a % b;
a = temp;
}
int max = a;
//최소공배수
int min = numbers[0] * numbers[1] / max;
System.out.println(max);
System.out.println(min);
}
}


최대공약수는 유클리드 호제법을 이용해서 구하고,


최소공배수는 A*B = L*G 임을 이용해서 구한다.


최소공배수를 구할 때는 자료형에 주의하자.




let input = readLine()!.split(separator: " ").map { Int($0)! }

var a = input.first!

var b = input.last!

while b != 0 {

    let temp = b

    b = a % b

    a = temp

}

let max = a

let min = input.first! * input.last! / max

print(max)

print(min)


Swift 쓰고 싶어...

'Algorithm > Baekjoon Online Judge' 카테고리의 다른 글

9613번 'GCD 합'  (0) 2018.07.31
1934번 '최소공배수'  (0) 2018.07.31
1912번 '연속합'  (0) 2018.07.30
11722번 '가장 긴 감소하는 부분 수열'  (0) 2018.07.29
11055번 '가장 큰 증가 부분 수열'  (0) 2018.07.29
댓글
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
TAG
more
«   2025/02   »
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
글 보관함