[Programmers] 최대공약수와 최소공배수 - 연습문제

동민·2021년 3월 10일
// 최대공약수와 최소공배수 - 연습문제
public class GCD_LCM {
	public int[] solution(int n, int m) {

		int[] answer = new int[2];

		int gcd = 0;

		for (int i = Math.min(n, m); i >= 1; i--) {
			if (n % i == 0 && m % i == 0) {
				gcd = i;
				break;
			}
		}
		answer[0] = gcd;
		answer[1] = n * m / gcd;

		return answer;

	}

	public static void main(String[] args) {

		GCD_LCM s = new GCD_LCM();

		for (int i = 0; i < s.solution(3, 12).length; i++) {
			System.out.print(s.solution(3, 12)[i] + " ");
		}
		System.out.println();
		for (int i = 0; i < s.solution(2, 5).length; i++) {
			System.out.print(s.solution(2, 5)[i] + " ");
		}

	}

}
profile
BE Developer

0개의 댓글