Skip to content

Commit 6ba9275

Browse files
committed
프로그래머스 최대공약수와 최소공배수 완료
1 parent 97c6ed8 commit 6ba9275

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
lines changed

Diff for: README.md

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1-
# [문제명](/)
1+
# [최대공약수와 최소공배수](https://programmers.co.kr/learn/courses/30/lessons/12940)
22

33
## 요구사항
44

5+
두 수의 최대공약수와 최소공배수를 반환
6+
[0]에 최대공약수, [1]에 최소공배수를 넣은 배열을 반환
7+
58
## 제한사항
69

10+
1 이상 1000000 이하
11+
712
## 기타

Diff for: SourceCode.java

+25-3
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,40 @@
1+
import java.util.Arrays;
2+
13
class Solution {
4+
public int[] solution(int n, int m) {
5+
int gcd = gcd(n, m);
6+
int lcm = n * m / gcd;
7+
8+
return new int[]{gcd, lcm};
9+
}
10+
11+
private int gcd(int a, int b) {
12+
int x = Math.max(a, b);
13+
int y = Math.min(a, b);
14+
15+
int mod = x % y;
16+
17+
if (mod == 0) {
18+
return y;
19+
}
20+
21+
return gcd(y, mod);
22+
}
223
}
324

425
public class SourceCode {
526
public static void main(String[] args) {
627
Object[] inputs = new Object[]{
7-
new Object[]{}
28+
new Object[]{3, 12},
29+
new Object[]{2, 5}
830
};
931

1032
for (Object input : inputs) {
1133
Object[] arguments = (Object[]) input;
1234

35+
int[] result = new Solution().solution((int) arguments[0], (int) arguments[1]);
1336

14-
15-
System.out.println();
37+
System.out.println(Arrays.toString(result));
1638
}
1739
}
1840
}

0 commit comments

Comments
 (0)