728x90
SMALL

using System.Collections.Generic;

public class Solution {
    public int[] solution(int n, int m) {
        int[] answer = new int[2];

        for(int i = (n<=m ? m : n);i>0;i--) /// 최소 공배수 구하기
        {
            if(n%i == 0 && m%i == 0)
            {
                answer[0] = i;
                break;
            }
        }
        
        for(int i = 1;i<=m*n;i++) //최대 공약수 구하기
        {
            if(i%n == 0 && i%m == 0)
            {
                answer[1] = i;
                break;
            }
        }
        
        return answer;
    }
}

 

////

 

public class Solution {
    public int[] solution(int n, int m) {
        int _gcd = gcd(n, m);
        int[] answer = new int[] {_gcd, n * m / _gcd};

        return answer;
    }

    int gcd(int a, int b)
    {
        return (a % b == 0 ? b : gcd(b, a % b));
    }
}
728x90

설정

트랙백

댓글