寒假程序设计与算法设计训练2——2020-01-02补充训练(递归算法)
2021/6/8 20:21:44
本文主要是介绍寒假程序设计与算法设计训练2——2020-01-02补充训练(递归算法),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
寒假程序设计与算法设计训练补充练习:求解递归函数例题1:POJ1664 放苹果
POJ1664 放苹果
我的AC代码:
#define _CRT_SECURE_NO_WARNINGS #include <cstdio> #define LL long long LL schemes(int m, int n) { if (0 == m || 1 == n) return 1; if (n > m) return schemes(m, m); else return schemes(m - n, n) + schemes(m, n - 1); } int main() { int t, m, n; scanf("%d", &t); while (t--) { scanf("%d %d", &m, &n); printf("%lld\n", schemes(m, n)); } return 0; }第二篇:求解递归数据
例题2:POJ 2013 Symmetric Order
POJ 2013 Symmetric Order
我的AC代码1:借助栈与队列
#include <iostream> #include <string> #include <algorithm> #include <stack> #include <queue> using namespace std; int main() { int n, kase = 1; while (cin >> n && n) { stack<string> nameStack; queue<string> nameQueue; for (int i = 0; i < n; ++i) { string name; cin >> name; if (i & 1) nameStack.push(name); else nameQueue.push(name); } cout << "SET " << kase++ << endl; while (!nameQueue.empty()) { cout << nameQueue.front() << endl; nameQueue.pop(); } while (!nameStack.empty()) { cout << nameStack.top() << endl; nameStack.pop(); } } return 0; }
例题3:POJ 2083 Fractal
POJ 2083 Fractal
我的AC代码:
#include <iostream> #include <cstring> using namespace std; char mp[731][731]; int a[8] = { 0, 1, 3, 9, 27, 81, 243, 729 }; void getMaze(int n, int x, int y) { if (1 == n) { mp[x][y] = 'X'; return; } else { getMaze(n - 1, x, y);// 左上角 getMaze(n - 1, x + a[n - 1], y + a[n - 1]);// 中间 getMaze(n - 1, x + 2 * a[n - 1], y);// 右上角 getMaze(n - 1, x, y + 2 * a[n - 1]);// 左下角 getMaze(n - 1, x + 2 * a[n - 1], y + 2 * a[n - 1]);// 右下角 } } int main() { int n; while (cin >> n && -1 != n) { for (int i = 0; i < 731; ++i) for (int j = 0; j < 731; ++j) mp[i][j] = ' '; getMaze(n, 1, 1); for (int i = 1; i <= a[n]; ++i) { for (int j = 1; j <= a[n]; ++j) cout << mp[i][j]; cout << endl; } cout << "-" << endl; } return 0; }
这篇关于寒假程序设计与算法设计训练2——2020-01-02补充训练(递归算法)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-06-26结对编程到底难不难?答案在这里
- 2024-06-19《2023版Java工程师》课程升级公告
- 2024-06-15matplotlib作图不显示3D图,怎么办?
- 2024-06-1503-Loki 日志监控
- 2024-06-1504-让LLM理解知识 -Prompt
- 2024-06-05做软件测试需要懂代码吗?
- 2024-06-0514-ShardingSphere的分布式主键实现
- 2024-06-03为什么以及如何要进行架构设计权衡?
- 2024-05-31全网首发第二弹!软考2024年5月《软件设计师》真题+解析+答案!(11-20题)
- 2024-05-31全网首发!软考2024年5月《软件设计师》真题+解析+答案!(21-30题)