반응형
https://www.acmicpc.net/problem/2609
1. gcd 함수 사용X
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class Main {
public static void main(String[] args)throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine()," ");
int[] a=new int[2];
ArrayList<Integer> lst = new ArrayList<Integer>();
a[0]=Integer.parseInt(st.nextToken());
a[1]=Integer.parseInt(st.nextToken());
Arrays.sort(a);
for(int i=1;i<=a[1];i++) {
if(a[0]%i==0&&a[1]%i==0) {
lst.add(i);
a[0]/=i;
a[1]/=i;
i=1;
}
}
int res=1;
for(int e: lst)
res*=e;
System.out.println(res);
System.out.println(res*a[0]*a[1]);
}
}
2. gcd 함수 사용O
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigInteger;
import java.util.*;
public class Main {
public static int gcd(int a, int b) {
if (b == 0) return a;
else return gcd(b, a % b);
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int a = Integer.parseInt(st.nextToken());
int b = Integer.parseInt(st.nextToken());
System.out.println(gcd(a, b));
System.out.println(gcd(a, b) * a / (gcd(a, b)) * b / (gcd(a, b)));
}
}
728x90
반응형
'코딩테스트 > 백준' 카테고리의 다른 글
백준 1292 Java 자바 (0) | 2022.08.03 |
---|---|
백준 2693 java 자바 (0) | 2022.08.03 |
백준 2309 Java 자바 (0) | 2022.08.02 |
백준 10870 Java (0) | 2022.08.02 |
백준 2460 Java (0) | 2022.08.01 |
댓글