유클리드 호제법을 활용하여 최대공약수를 계산하는 작업이 필요한 문제입니다.
import java.util.Arrays;
public class Solution {
public int[] solution(int numer1, int denom1, int numer2, int denom2) {
int[] answer = new int[2];
if(denom1==denom2){
return checkFraction(numer1+numer2,denom1);
}
int gcd=myGCD(numer1*denom2+numer2*denom1,denom1*denom2);
return new int[] {(numer1*denom2+numer2*denom1)/gcd,denom1*denom2/gcd};
}
private int[] checkFraction(int numerSum,int denom) {
if(numerSum%denom==0){
return new int[] {numerSum/denom,1};
}
int gcd=myGCD(numerSum,denom);
return new int[]{numerSum/gcd, denom/gcd};
}
private int myGCD(int num1,int num2) {
if (num2 == 0) {
return num1;
}
return myGCD(num2, num1 % num2);
}
}