반응형
문제 |
소스 |
import java.util.Scanner;
public class Main {
// 최대공약수 (Greatest Common Divisor)
public static int gcd(int x, int y) {
// 유클리드 (x를 y로 나눈 나머지가 0보다 클때까지 반복)
while(y > 0) {
int temp = y;
y = x % y;
x = temp;
}
return x;
}
// 최소공배수 (Least Common Multiple)
public static int lcm(int x, int y) {
// 0이 아닌 두 수의 곱 / 두 수의 최대공약수
return (x * y) / gcd(x, y);
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
System.out.println(gcd(a,b));
System.out.println(lcm(a,b));
}
}출처 |
반응형
'알고리즘 > JAVA' 카테고리의 다른 글
| [JAVA] 백준 알고리즘 2455 : 지능형 기차 (0) | 2018.08.28 |
|---|---|
| [JAVA] 백준 알고리즘 1978 : 소수 찾기 (0) | 2018.08.28 |
| [JAVA] 백준 알고리즘 7576 : 토마토 (BFS) (0) | 2018.08.28 |
| [JAVA] 백준 알고리즘 2178 : 미로 탐색 (BFS) (1) | 2018.08.28 |
| [JAVA] 백준 알고리즘 1260 : DFS와 BFS (0) | 2018.08.24 |