/* 최대공약수와 최소공배수
* 두 수를 입력받아 두 수의 최대공약수와 최소공배수를 반환하는 함수,
* solution을 완성해 보세요.
* 배열의 맨 앞에 최대공약수, 그다음 최소공배수를 넣어 반환하면 됩니다.
* 예를 들어 두 수 3, 12의 최대공약수는 3,
* 최소공배수는 12이므로 solution(3, 12)는 [3, 12]를 반환해야 합니다.
*
* n m return
* 3 12 [3, 12]
* 2 5 [1, 10]
*/
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | public class programmer_1_36 { static int a1 = 3; static int b1 = 12; static int a2 = 2; static int b2 = 5; public int getMax(int n, int m){ int max = m > n ? m : n; for(int i = max; i > 0; i--){ if(m % i == 0 && n % i == 0){ max = i; break; } } return max; } public int[] solution(int n, int m) { int[] answer = new int[2]; answer[0] = getMax(n,m); answer[1] = n * m / answer[0]; return answer; } public static void main(String args[]){ programmer_1_36 t = new programmer_1_36(); System.out.println("---------------------------------------"); System.out.println("result = " + t.solution(a1,b1)); System.out.println("---------------------------------------"); System.out.println("result = " + t.solution(a2,b2)); System.out.println("---------------------------------------"); } } | cs |
'프로그래머스 > lv1' 카테고리의 다른 글
이상한 문자 만들기 (0) | 2022.12.20 |
---|---|
같은 숫자는 싫어 (1) | 2022.12.20 |
직사각형 별찍기 (0) | 2022.12.19 |
행렬의 덧셈 (0) | 2022.12.19 |
부족한 금액 계산하기 (1) | 2022.12.19 |