钢条切割
来源:互联网 发布:进程调度算法实验报告 编辑:程序博客网 时间:2024/06/11 03:48
#include <iostream>#include <vector>#include <algorithm>#include <iterator>using namespace std;int cutRod(int *p, int n){if (n == 0)return 0;int q = -1;for(int i = 1; i <= n; i++)q = max(q, p[i]+cutRod(p, n-i)); return q;}//自顶向下辅助函数 int memoizedCutRodAux(int *p, int n, int *r){if (r[n] >= 0)return r[n];int q = -1;if (n == 0)q = 0;else{for (int i = 1; i <= n; i++)q = max(q, p[i]+memoizedCutRodAux(p, n-i, r));}r[n] = q;return q;}//带备忘录的自顶向下,用r[i]记录已经求出的最优解,避免重复调用 int memoizedCutRod(int *p, int n){int r[11] = {};for (int i = 0; i <= n; i++)r[i] = -1;int q = memoizedCutRodAux(p, n, r);return q; }//自底向下 int bottomUpCutRod(int *p, int n){int r[11] = {0};int q; for (int j = 1; j <= n; j++){q = -1;for (int i = 1; i <= j; i++)q = max(q, p[i]+r[j-i]);r[j] = q;}return r[n]; }//下面使用的是vector...都生疏了...T_T//r[]: 记录最优解//s[]:记录最优解的第一段的钢条的切割长度pair<vector<int>, vector<int> > extendedBottomUpCutRod(vector<int> p, int n){vector<int> r, s(p.size());int q = -1;r.push_back(0);for (int j = 1; j <= n; j++){q = -1;int i;for (i = 1; i <= j; i++){ if (q < p[i] + r[j-i]) { q = p[i] + r[j-i]; s[j] = i; }}r.push_back(q);}return make_pair(r, s);}void printCutRodSolution(vector<int> p, int n){pair<vector<int>, vector<int> > vvp =extendedBottomUpCutRod(p, n);cout << vvp.first.at(n) << endl;vector<int> s = vvp.second;while (n > 0){cout << s[n] << endl;n -= s[n];}}int main(){//使price[0]为0,在程序里不使用, //只是为了使截断的左右两端与数组下标保持一致,方便代码 int price[] = {0, 1, 5, 8, 9, 10, 17, 17, 20, 24, 30};int n;cout << "请输入n (ctrl+z结束):";while (cin >> n){cout << "n=" << n << endl;cout << "普通递归法:\n" << cutRod(price, n) << endl;cout << "带备忘录的自顶向下法:\n" << memoizedCutRod(price, n) << endl; cout << "自底向下法:\n" << bottomUpCutRod(price, n) << endl;vector<int> p(price, price+sizeof(price)/sizeof(price[0]));cout << "扩展的自底向下法:" << endl;printCutRodSolution(p, n);cout << endl << "请输入n (ctrl+z结束): ";}system("pause");return 0;}
0 0
- 钢条切割
- 钢条切割
- 钢条切割
- 钢条切割-动态规划
- 钢条切割问题
- 钢条切割--【算法导论】
- 钢条切割问题
- 切割钢条【动态规划】
- 动态规划-钢条切割
- 动态规划-钢条切割
- 钢条切割问题
- 钢条切割问题
- 动态规划-钢条切割
- 动态规划 钢条切割
- 钢条切割-动态规划
- CLRS 15.1钢条切割
- 【动态规划】钢条切割
- 动态规划--钢条切割
- 输出字母图形
- 面试题16:反转链表
- Python数据类型 -- 列表习题
- Xcode6中"无法代码提示"解决方法
- RTP与RTSP的区别
- 钢条切割
- Python数据类型 -- 元组合集合
- 当 IDENTITY_INSERT 设置为 OFF 时,不能为表中的标识列插入显式值
- The Implementation of A2DP Sink in Android 4.4
- 利用PHP批量修改文件名称
- 免费开源的Flash反编译软件
- Ubuntu 卸载 LibreOffice
- MAXPIECESIZE与FORMAT参数设置不合理导致RMAN备份失败
- Android 高手进阶之自定义View,自定义属性(带进度的圆形进度条)