Skip to content

Commit 929e056

Browse files
committed
문제 정리 구조, 작성 예시 작성 완료
1 parent 8978d1c commit 929e056

6 files changed

Lines changed: 194 additions & 0 deletions

File tree

assets/example/P_2609/README.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
2+
---
3+
4+
## 🔖 문제 설명
5+
6+
- 주어진 두 수의 최대 공약수와 최소 공배수를 구하는 문제이다.
7+
- `link` : [`click`](https://www.acmicpc.net/problem/2609)
8+
9+
---
10+
11+
## 🍳 스스로 생각한 접근 방식
12+
13+
문제를 보니 유클리드 호제법을 이용하면 좋겠다는 생각이 들었다.
14+
15+
> 💡 유클리드 호제법이란?
16+
최대 공약수를 구하는 대표적인 알고리즘으로, **“두 수 a, b (a > b) 가 있을 때, 이 둘 간의 서로소 r 은 b 와 (a % b) 의 서로소와 같다”** 는 법칙이다.
17+
자세한 내용은 [`[1]`](#1--euclidean-algorithm---wikepedia) 을 참고하는 것이 좋을 듯 하다.
18+
19+
호제법을 이용하면 아주 간편히 최대 공약수를 알아낼 수 있다. 그런데 생각해보니 최대 공약수를 이용하면 최대 공배수 또한 알 수 있었다.
20+
21+
두 수 $A, B$ 의 최대 공약수를 $r$ 이라 하면, $A = a \times r, \ \ B = b \times r$ 로 나타낼 수 있다.
22+
23+
이 때 두 수의 최대 공배수는 $a \times b \times r = A \times B \div r$ 로 나타낼 수 있다.
24+
25+
결국 유클리드 호제법만 잘 구현하면 끝나는 문제인 것이다.
26+
27+
---
28+
29+
30+
## ❗ 틀린 이유 설명
31+
32+
`(올바르게 문제를 풀이했다)`
33+
34+
---
35+
36+
37+
## ✅ 올바른 접근 방식 및 해결 방식
38+
39+
`(올바르게 문제를 풀이했다)`
40+
41+
---
42+
43+
## 🛠 자신의 풀이에서 개선할 부분
44+
45+
유클리드 호제법의 시간 복잡도를 생각해 보았다.
46+
47+
직관적으로 생각했을 때, $O(\mathbf{log}(N))$ 과 유사할 것이라 생각하였다. 애초에 유클리드 호제법은 두 수의 `modulo` 연산으로 이뤄지기 때문이다.
48+
49+
찾아본 결과 [`[2]`](#2--euclidean-algorithm---codility) 와 같은 글을 볼 수 있었고, 이에 따르면 호제법은 $O(\mathbf{log(max}(a, b)))$ 의 시간 복잡도를 가진다 한다.
50+
51+
---
52+
53+
## Reference
54+
55+
- ##### [`[1] : Euclidean algorithm - Wikepedia`](https://en.wikipedia.org/wiki/Euclidean_algorithm)
56+
- ##### [`[2] : Euclidean algorithm - Codility`](https://codility.com/media/train/10-Gcd.pdf)
57+
58+
---
59+
60+
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package assets.example.P_2609;
2+
3+
import java.io.BufferedReader;
4+
import java.io.IOException;
5+
import java.io.InputStreamReader;
6+
import java.util.StringTokenizer;
7+
8+
public class Solution_2609 {
9+
10+
private static final BufferedReader br = new BufferedReader(
11+
new InputStreamReader(System.in)
12+
);
13+
14+
15+
public static void main(String[] args) throws IOException {
16+
StringTokenizer tokenizer = new StringTokenizer(br.readLine());
17+
18+
int num1 = Integer.parseInt(tokenizer.nextToken());
19+
int num2 = Integer.parseInt(tokenizer.nextToken());
20+
21+
int gcd = GCD(num1, num2);
22+
int lcm = num1 * num2 / gcd;
23+
24+
System.out.println(gcd);
25+
System.out.println(lcm);
26+
}
27+
28+
public static int GCD(int a, int b) { // get Greatest common divisor using euclidean algorithm
29+
while (b != 0) {
30+
int mod = a % b;
31+
a = b;
32+
b = mod;
33+
}
34+
35+
return a;
36+
}
37+
}
58 KB
Loading
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
2+
---
3+
4+
## 🔖 문제 설명
5+
6+
- 주어진 두 수의 최대 공약수와 최소 공배수를 구하는 문제이다.
7+
- `link` : [`click`](https://www.acmicpc.net/problem/2609)
8+
9+
---
10+
11+
## 🍳 스스로 생각한 접근 방식
12+
13+
문제를 보니 유클리드 호제법을 이용하면 좋겠다는 생각이 들었다.
14+
15+
> 💡 유클리드 호제법이란?
16+
최대 공약수를 구하는 대표적인 알고리즘으로, **“두 수 a, b (a > b) 가 있을 때, 이 둘 간의 서로소 r 은 b 와 (a % b) 의 서로소와 같다”** 는 법칙이다.
17+
자세한 내용은 [`[1]`](#1--euclidean-algorithm---wikepedia) 을 참고하는 것이 좋을 듯 하다.
18+
19+
호제법을 이용하면 아주 간편히 최대 공약수를 알아낼 수 있다. 그런데 생각해보니 최대 공약수를 이용하면 최대 공배수 또한 알 수 있었다.
20+
21+
두 수 $A, B$ 의 최대 공약수를 $r$ 이라 하면, $A = a \times r, \ \ B = b \times r$ 로 나타낼 수 있다.
22+
23+
이 때 두 수의 최대 공배수는 $a \times b \times r = A \times B \div r$ 로 나타낼 수 있다.
24+
25+
결국 유클리드 호제법만 잘 구현하면 끝나는 문제인 것이다.
26+
27+
---
28+
29+
30+
## ❗ 틀린 이유 설명
31+
32+
`(올바르게 문제를 풀이했다)`
33+
34+
---
35+
36+
37+
## ✅ 올바른 접근 방식 및 해결 방식
38+
39+
`(올바르게 문제를 풀이했다)`
40+
41+
---
42+
43+
## 🛠 자신의 풀이에서 개선할 부분
44+
45+
유클리드 호제법의 시간 복잡도를 생각해 보았다.
46+
47+
직관적으로 생각했을 때, $O(\mathbf{log}(N))$ 과 유사할 것이라 생각하였다. 애초에 유클리드 호제법은 두 수의 `modulo` 연산으로 이뤄지기 때문이다.
48+
49+
찾아본 결과 [`[2]`](#2--euclidean-algorithm---codility) 와 같은 글을 볼 수 있었고, 이에 따르면 호제법은 $O(\mathbf{log(max}(a, b)))$ 의 시간 복잡도를 가진다 한다.
50+
51+
---
52+
53+
## Reference
54+
55+
- ##### [`[1] : Euclidean algorithm - Wikepedia`](https://en.wikipedia.org/wiki/Euclidean_algorithm)
56+
- ##### [`[2] : Euclidean algorithm - Codility`](https://codility.com/media/train/10-Gcd.pdf)
57+
58+
---
59+
60+
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package jbw9964.M_2024_04.Week_2.P_2609;
2+
3+
import java.io.BufferedReader;
4+
import java.io.IOException;
5+
import java.io.InputStreamReader;
6+
import java.util.StringTokenizer;
7+
8+
public class Solution_2609 {
9+
10+
private static final BufferedReader br = new BufferedReader(
11+
new InputStreamReader(System.in)
12+
);
13+
14+
15+
public static void main(String[] args) throws IOException {
16+
StringTokenizer tokenizer = new StringTokenizer(br.readLine());
17+
18+
int num1 = Integer.parseInt(tokenizer.nextToken());
19+
int num2 = Integer.parseInt(tokenizer.nextToken());
20+
21+
int gcd = GCD(num1, num2);
22+
int lcm = num1 * num2 / gcd;
23+
24+
System.out.println(gcd);
25+
System.out.println(lcm);
26+
}
27+
28+
public static int GCD(int a, int b) { // get Greatest common divisor using euclidean algorithm
29+
while (b != 0) {
30+
int mod = a % b;
31+
a = b;
32+
b = mod;
33+
}
34+
35+
return a;
36+
}
37+
}
58 KB
Loading

0 commit comments

Comments
 (0)