Deep_Dev
article thumbnail

 

 


์ตœ๋Œ€๊ณต์•ฝ์ˆ˜์™€ ์ตœ์†Œ๊ณต๋ฐฐ์ˆ˜๋ฅผ ์ด์šฉํ•˜์—ฌ ํ’€๋ฉด ๋œ๋‹ค.

๋‹ค๋งŒ ๋ฌธ์ œ์—์„œ๋Š” ์•ˆ๋‚˜์™€์žˆ์ง€๋งŒ, ๋ถ„์ˆ˜๋ผ๋ฆฌ ๋ง์…ˆํ•œ ๋ถ„์ˆ˜๊ฐ€ ์•ฝ๋ถ„์ด ๊ฐ€๋Šฅํ•˜๋‹ค๋ฉด ์•ฝ๋ถ„๊นŒ์ง€ ํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค . ( 33์ ์ด ๋‚˜์˜ค๋Š”๊ฒฝ์šฐ )

 

 

class Main {
    public int gcd(int a, int b){
        if (a%b==0) return b;
        return gcd(b,a%b);
    }
    public int lcm(int a, int b){
        return a*b/gcd(a,b);
    }
    public int[] solution(int denum1, int num1, int denum2, int num2) {
        int[] answer = new int[2];

        int min=lcm(num1,num2); // ์ตœ์†Œ ๊ณต๋ฐฐ์ˆ˜ -> ๋ถ„๋ชจ
        int top=((denum1*(min/num1))+denum2*(min/num2));

        // ์•ฝ๋ถ„์ด ๊ฐ€๋Šฅํ•˜๋‹ค๋ฉด
        int max= gcd(top,min); // ์ตœ๋Œ€ ๊ณต์•ฝ์ˆ˜
        if(max!=0){
            min/=max;
            top/=max;
        }
        answer[0]=top;
        answer[1]=min;
        return answer;
    }
}