diff --git a/oesnuj/DP/2775.cpp b/oesnuj/DP/2775.cpp new file mode 100644 index 0000000..c7cf2e6 --- /dev/null +++ b/oesnuj/DP/2775.cpp @@ -0,0 +1,53 @@ +/*풀이1 재귀만 이용*/ +//#include +//using namespace std; +// +//int calc(int k, int n) { +// if (k == 0) return n; +// int sum = 0; +// for (int i = n; i > 0; i--) { +// sum += calc(k - 1, i); +// } +// return sum; +//} +// +//int main() +//{ +// int T, k, n; +// cin >> T; +// while (T--) { +// cin >> k >> n; +// cout << calc(k, n) << "\n"; +// } +// return 0; +//} + +/*풀이2, 재귀 + DP*/ +#include +#include +using namespace std; + +vector> dp; + +int calc(int k, int n) { + if (k == 0) return n; + if (dp[k][n] != -1) return dp[k][n]; // 이미 계산된 값이면 반환 + + int sum = 0; + for (int i = n; i > 0; i--) { + sum += calc(k - 1, i); + } + return dp[k][n] = sum; // 계산된 값을 저장하고 반환 +} + +int main() +{ + int T, k, n; + cin >> T; + while (T--) { + cin >> k >> n; + dp = vector>(k + 1, vector(n + 1, -1)); //케이스별 DP 테이블 초기화 + cout << calc(k, n) << "\n"; + } + return 0; +} diff --git a/oesnuj/README.md b/oesnuj/README.md index 1052526..7d0b194 100644 --- a/oesnuj/README.md +++ b/oesnuj/README.md @@ -13,4 +13,6 @@ | 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) | +| 13차시 | 2024.07.28 | DP | [부녀회장이 될테야](https://www.acmicpc.net/problem/2775) | [#44](https://github.com/AlgoLeadMe/AlgoLeadMe-10/pull/44) | --- 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