반응형
최대공약수, 최소공배수 메서드 생성하여 출력하기
최대공약수는 유클리드 호제법을 사용하여 구함.
최소공배수는 (두 수의 곱) 나누기 (최대공약수)로 구함.
import java.util.Scanner;
public class BOJ2609 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int x=sc.nextInt();
int y=sc.nextInt();
System.out.println(GCD(x,y));
System.out.println(LCM(x,y));
}
public static int GCD(int a, int b){
while(b!=0){
int r=a%b;
a=b;
b=r;
}
return a;
}
public static int LCM(int a, int b){
int GCD=GCD(a,b);
return a*b/GCD;
}
}
반응형
'Coding Test(Algorithms)' 카테고리의 다른 글
[JAVA] 수학 연습문제 - 백준 1929 (0) | 2021.07.07 |
---|---|
[JAVA] 수학 연습문제 - 백준 1978 (0) | 2021.07.06 |
[JAVA] 수학 연습문제 - 백준 1934 (0) | 2021.07.04 |
[JAVA] 자료구조 연습문제 - 백준 17413 (0) | 2021.07.03 |
[JAVA] 자료구조 연습문제 - 백준 17299 (0) | 2021.07.02 |