From 6a14272c73c6bb70fbbc918b5ae9b6f12c55b405 Mon Sep 17 00:00:00 2001 From: Junseo Kim Date: Thu, 25 Jul 2024 04:33:50 +0900 Subject: [PATCH 1/2] =?UTF-8?q?2024-07-24=20=ED=95=98=EB=85=B8=EC=9D=B4=20?= =?UTF-8?q?=ED=83=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- oesnuj/README.md | 1 + "oesnuj/\354\236\254\352\267\200/1914.cpp" | 33 ++++++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 "oesnuj/\354\236\254\352\267\200/1914.cpp" diff --git a/oesnuj/README.md b/oesnuj/README.md index 1052526..bc15eeb 100644 --- a/oesnuj/README.md +++ b/oesnuj/README.md @@ -13,4 +13,5 @@ | 9차시 | 2024.05.30 | 구현 | [빙고](https://www.acmicpc.net/problem/2578) | [#30](https://github.com/AlgoLeadMe/AlgoLeadMe-10/pull/30) | | 10차시 | 2024.07.04 | 구현 | [상어초등학교](https://www.acmicpc.net/problem/21608) | [#34](https://github.com/AlgoLeadMe/AlgoLeadMe-10/pull/34) | | 11차시 | 2024.07.18 | 이분 탐색 | [랜선 자르기](https://www.acmicpc.net/problem/1654) | [#38](https://github.com/AlgoLeadMe/AlgoLeadMe-10/pull/38) | +| 12차시 | 2024.07.24 | 재귀 | [하노이 탑](https://www.acmicpc.net/problem/1914) | [#41](https://github.com/AlgoLeadMe/AlgoLeadMe-10/pull/41) | --- diff --git "a/oesnuj/\354\236\254\352\267\200/1914.cpp" "b/oesnuj/\354\236\254\352\267\200/1914.cpp" new file mode 100644 index 0000000..9074b25 --- /dev/null +++ "b/oesnuj/\354\236\254\352\267\200/1914.cpp" @@ -0,0 +1,33 @@ +#include +#include +#include +using namespace std; + +void RecurMoveDisks(int n, int from, int temp, int to) { + if (n == 0) return; + //n-1개의 원판으로 temp로 옮긴다. + RecurMoveDisks(n - 1, from, to, temp); + + //남은 1개의 원판으로 to로 옮긴다. <- 이때 옮기는 과정 출력 + cout << from + 1 << " " << to + 1 << "\n"; + + //temp에 있던 n-1개의 원판을 n-1개로 옮긴다. + RecurMoveDisks(n - 1, temp, from, to); +} + +int main() { + ios::sync_with_stdio(false); cin.tie(nullptr); + int n; + cin >> n; + + //2의 n제곱 -1 출력하기 + string cnt = to_string(pow(2, n)); + int x = cnt.find('.'); + cnt = cnt.substr(0, x); + cnt[cnt.length() - 1] -= 1; + cout << cnt << "\n"; + + //조건에 맞는 경우에만 재귀를 돌린다. + if (n <= 20) RecurMoveDisks(n, 0, 1, 2); + return 0; +} \ No newline at end of file From fc20d4239374dac116be46d5823bb5772450289f Mon Sep 17 00:00:00 2001 From: Junseo Kim Date: Thu, 25 Jul 2024 05:31:45 +0900 Subject: [PATCH 2/2] Update 1914.cpp --- "oesnuj/\354\236\254\352\267\200/1914.cpp" | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git "a/oesnuj/\354\236\254\352\267\200/1914.cpp" "b/oesnuj/\354\236\254\352\267\200/1914.cpp" index 9074b25..90b8165 100644 --- "a/oesnuj/\354\236\254\352\267\200/1914.cpp" +++ "b/oesnuj/\354\236\254\352\267\200/1914.cpp" @@ -9,7 +9,7 @@ void RecurMoveDisks(int n, int from, int temp, int to) { RecurMoveDisks(n - 1, from, to, temp); //남은 1개의 원판으로 to로 옮긴다. <- 이때 옮기는 과정 출력 - cout << from + 1 << " " << to + 1 << "\n"; + cout << from << " " << to << "\n"; //temp에 있던 n-1개의 원판을 n-1개로 옮긴다. RecurMoveDisks(n - 1, temp, from, to); @@ -28,6 +28,6 @@ int main() { cout << cnt << "\n"; //조건에 맞는 경우에만 재귀를 돌린다. - if (n <= 20) RecurMoveDisks(n, 0, 1, 2); + if (n <= 20) RecurMoveDisks(n, 1, 2, 3); return 0; } \ No newline at end of file