File tree 2 files changed +31
-4
lines changed
2 files changed +31
-4
lines changed Original file line number Diff line number Diff line change 1
- # [ 문제명 ] ( / )
1
+ # [ 최대공약수와 최소공배수 ] ( https://programmers.co.kr/learn/courses/30/lessons/12940 )
2
2
3
3
## 요구사항
4
4
5
+ 두 수의 최대공약수와 최소공배수를 반환
6
+ [ 0] 에 최대공약수, [ 1] 에 최소공배수를 넣은 배열을 반환
7
+
5
8
## 제한사항
6
9
10
+ 1 이상 1000000 이하
11
+
7
12
## 기타
Original file line number Diff line number Diff line change
1
+ import java .util .Arrays ;
2
+
1
3
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
+ }
2
23
}
3
24
4
25
public class SourceCode {
5
26
public static void main (String [] args ) {
6
27
Object [] inputs = new Object []{
7
- new Object []{}
28
+ new Object []{3 , 12 },
29
+ new Object []{2 , 5 }
8
30
};
9
31
10
32
for (Object input : inputs ) {
11
33
Object [] arguments = (Object []) input ;
12
34
35
+ int [] result = new Solution ().solution ((int ) arguments [0 ], (int ) arguments [1 ]);
13
36
14
-
15
- System .out .println ();
37
+ System .out .println (Arrays .toString (result ));
16
38
}
17
39
}
18
40
}
You can’t perform that action at this time.
0 commit comments