문제
내가 작성한 정답
class Solution {
public int[] solution(int numer1, int denom1, int numer2, int denom2) {
int cd = denom1*denom2;
int cn = numer1*denom2+numer2*denom1;
int c = gcd(cd,cn);
return new int[]{cn/c,cd/c};
}
private int gcd(int a,int b){
while(b!=0){
int tem = b;
b = a%b;
a = tem;
}
return a;
}
}다른 사람들의 정답
class Solution {
public int[] solution(int denum1, int num1, int denum2, int num2) {
int mother = num1 * num2;
int son1 = num1 * denum2;
int son2 = num2 * denum1;
int totalSon = son1 + son2;
for(int i = mother; i >= 1; i--){
if(totalSon % i == 0 && mother % i == 0){
totalSon /= i;
mother /= i;
}
}
int[] answer = {totalSon, mother};
return answer;
}
}Share article