package com.company;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Solution {
static public void main(String[] args) {
int n1 = 3;
int m1 = 12;
int n2 = 2;
int m2 = 5;
System.out.println(Arrays.toString(solution(n1, m1)));
System.out.println(Arrays.toString(solution(n2, m2)));
System.out.println(Arrays.toString(solution(4, 8)));
System.out.println(Arrays.toString(solution(5, 10)));
System.out.println(Arrays.toString(solution(9, 15)));
System.out.println(Arrays.toString(solution(18, 30)));
}
static public int[] solution(int n, int m){
int[] answer = {0,0};
List<Integer> nArray = new ArrayList<Integer>();
List<Integer> mArray = new ArrayList<Integer>();
List<Integer> divisorsList = new ArrayList<Integer>();
for (int i = 1; i <= n; i++) {
if(n % i == 0) {
nArray.add(i);
}
}
for (int i = 1; i <= m; i++) {
if(m % i == 0) {
mArray.add(i);
}
}
for (int divisor : nArray) {
if(mArray.contains(divisor)){
divisorsList.add(divisor);
}
}
answer[0] = divisorsList.get(divisorsList.size() - 1);
int shareOfN = n / answer[0];
int shareOfM = m / answer[0];
answer[1] = answer[0] * shareOfN * shareOfM;
return answer;
}
}
내가 작성한 코드보다 더 나은 코드가 있어서 가져옴
public long[] solution(int n, int m) {
long[] answer = new long [2];
if(m%n==0){
answer[0] = n;
answer[1] = m;
}else{
for(int i = 1; i <= m; i++){
if(n%i==0 && m%i==0){
answer[0] = i;
}
}
answer[1] = n*m/answer[0];
}
return answer;
}