From 046b10bb702270ffe9dc61998718703f9ea99e6c Mon Sep 17 00:00:00 2001 From: sh0723 Date: Tue, 22 Sep 2026 17:57:08 +0900 Subject: [PATCH 1/2] =?UTF-8?q?12852=20:=201=EB=A1=9C=20=EB=A7=8C=EB=93=A4?= =?UTF-8?q?=EA=B8=B0=202=20solve?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/week7/BOJ12852/BOJ12852_V1.cpp | 38 ++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 src/week7/BOJ12852/BOJ12852_V1.cpp diff --git a/src/week7/BOJ12852/BOJ12852_V1.cpp b/src/week7/BOJ12852/BOJ12852_V1.cpp new file mode 100644 index 0000000..7883025 --- /dev/null +++ b/src/week7/BOJ12852/BOJ12852_V1.cpp @@ -0,0 +1,38 @@ +#include +using namespace std; +int dp[1000004]; +void solve(int n) { + if (n == 0) return; + cout << n << ' '; + if (n%3 == 0 && dp[n/3] + 1 == dp[n]) solve(n/3); + else if (n%2 == 0 && dp[n/2] + 1 == dp[n]) solve(n/2); + else if (n > 1 && dp[n-1] + 1 == dp[n]) solve(n-1); + + return; +} +int main() { + + ios_base::sync_with_stdio(false); + cin.tie(nullptr); + cout.tie(nullptr); + + int n; + cin >> n; + + fill(dp, dp+1000004, INT_MAX); + + dp[1] = 0; + + for (int i=2; i<=n; i++) { + if (i%3 == 0) dp[i] = min(dp[i/3] + 1, dp[i]); + if (i%2 == 0) dp[i] = min(dp[i/2] + 1, dp[i]); + + dp[i] = min(dp[i-1] + 1, dp[i]); + } + + cout << dp[n] << '\n'; + solve(n); + + + return 0; +} \ No newline at end of file From b525d4984a6829c1423d8ea172427fea00a17adc Mon Sep 17 00:00:00 2001 From: sh0723 Date: Tue, 22 Sep 2026 18:02:09 +0900 Subject: [PATCH 2/2] =?UTF-8?q?12852=20:=201=EB=A1=9C=20=EB=A7=8C=EB=93=A4?= =?UTF-8?q?=EA=B8=B0=202=20=EC=B6=9C=EB=A0=A5=20=EC=A0=95=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/week7/BOJ12852/BOJ12852_V1.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/week7/BOJ12852/BOJ12852_V1.cpp b/src/week7/BOJ12852/BOJ12852_V1.cpp index 7883025..3f6c3a6 100644 --- a/src/week7/BOJ12852/BOJ12852_V1.cpp +++ b/src/week7/BOJ12852/BOJ12852_V1.cpp @@ -32,7 +32,8 @@ int main() { cout << dp[n] << '\n'; solve(n); + cout << '\n'; return 0; -} \ No newline at end of file +}