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..90b8165 --- /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 << " " << to << "\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, 1, 2, 3); + return 0; +} \ No newline at end of file