Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 8b075ba

Browse files
Create BOJ1463.md
1 parent f027a63 commit 8b075ba

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed

‎challenges/BOJ1463.md‎

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# 문제
2+
1로 만들기
3+
4+
## 문제 원본
5+
문제의 원본은 [여기서](https://www.acmicpc.net/problem/1463) 확인하세요.
6+
7+
## 분류
8+
* 다이나믹 프로그래밍
9+
10+
# 풀이
11+
12+
```
13+
dp[n] : n을 1로 만드는데 필요한 최소 연산 횟수
14+
```
15+
16+
* 3으로 나누어 떨어지는 경우
17+
```
18+
dp[i] = dp[i / 3] + 1
19+
```
20+
21+
* 2로 나누어 떨어지는 경우
22+
```
23+
dp[i] = dp[i / 2] + 1
24+
```
25+
26+
* 1을 뺴는 경우
27+
```
28+
dp[i] = dp[i - 1] + 1;
29+
```
30+
31+
위의 경우들의 최솟값으로 dp[i]를 채운다. 이 과정 이후에 dp[n]이 해가 된다.
32+
33+
``` c++
34+
#include <iostream>
35+
#include <vector>
36+
37+
using namespace std;
38+
39+
int main(void) {
40+
ios::sync_with_stdio(false);
41+
cin.tie(0); cout.tie(0);
42+
43+
int n;
44+
cin >> n;
45+
46+
vector<int> dp(n + 3);
47+
dp[0] = 0;
48+
dp[1] = 0;
49+
dp[2] = 1;
50+
dp[3] = 1;
51+
52+
for (int i = 4; i <= n; i++) {
53+
dp[i] = dp[i - 1] + 1;
54+
if (i % 3 == 0) {
55+
dp[i] = min(dp[i], dp[i / 3] + 1);
56+
}
57+
if (i % 2 == 0) {
58+
dp[i] = min(dp[i], dp[i / 2] + 1);
59+
}
60+
}
61+
62+
cout << dp[n] << endl;
63+
64+
return 0;
65+
}
66+
```

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /