forked from ndb796/python-for-coding-test
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path5.cpp
38 lines (32 loc) ยท 1018 Bytes
/
5.cpp
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
#include <bits/stdc++.h>
using namespace std;
int n;
int ugly[1000]; // ๋ชป์๊ธด ์๋ฅผ ๋ด๊ธฐ ์ํ ํ
์ด๋ธ (1์ฐจ์ DP ํ
์ด๋ธ)
int main(void) {
cin >> n;
// 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] = min(next2, 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๋ฒ์งธ ๋ชป์๊ธด ์๋ฅผ ์ถ๋ ฅ
cout << ugly[n - 1] << '\n';
}