forked from ndb796/python-for-coding-test
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path5.java
40 lines (34 loc) ยท 1.22 KB
/
5.java
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
30
31
32
33
34
35
36
37
38
39
40
import java.util.*;
public class Main {
static int n;
static int[] ugly = new int[1000]; // ๋ชป์๊ธด ์๋ฅผ ๋ด๊ธฐ ์ํ ํ
์ด๋ธ (1์ฐจ์ DP ํ
์ด๋ธ)
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
// 2๋ฐฐ, 3๋ฐฐ, 5๋ฐฐ๋ฅผ ์ํ ์ธ๋ฑ์ค
int i2 = 0, i3 = 0, i5 = 0;
// ์ฒ์์ ๊ณฑ์
๊ฐ์ ์ด๊ธฐํ
int next2 = 2, next3 = 3, next5 = 5;
ugly[0] = 1; // ์ฒซ ๋ฒ์งธ ๋ชป์๊ธด ์๋ 1
// 1๋ถํฐ n๊น์ง์ ๋ชป์๊ธด ์๋ค์ ์ฐพ๊ธฐ
for (int l = 1; l < n; l++) {
// ๊ฐ๋ฅํ ๊ณฑ์
๊ฒฐ๊ณผ ์ค์์ ๊ฐ์ฅ ์์ ์๋ฅผ ์ ํ
ugly[l] = Math.min(next2, Math.min(next3, next5));
// ์ธ๋ฑ์ค์ ๋ฐ๋ผ์ ๊ณฑ์
๊ฒฐ๊ณผ๋ฅผ ์ฆ๊ฐ
if (ugly[l] == next2) {
i2 += 1;
next2 = ugly[i2] * 2;
}
if (ugly[l] == next3) {
i3 += 1;
next3 = ugly[i3] * 3;
}
if (ugly[l] == next5) {
i5 += 1;
next5 = ugly[i5] * 5;
}
}
// n๋ฒ์งธ ๋ชป์๊ธด ์๋ฅผ ์ถ๋ ฅ
System.out.println(ugly[n - 1]);
}
}