Introduction to C + + implementation algorithm 15 chapter of the Steel bar segmentation in dynamic programming

Source: Internet
Author: User

#include <iostream> #include <algorithm> #include <utility> #include <vector>using namespace std;//uses a common recursive algorithm to solve the largest yield of the steel bar segmentation int Cut_rod (int *p,const int &n) {if (n==0) return 0;int q=-1;for (int i=1;i<=n;++i) {Q =max (Q,p[i]+cut_rod (P,n-i));} return q;} A dynamic programming function method with a top-down function to solve the problem int memoized_cut_rod_aux (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]+memoized_cut_rod_aux (P,n-i,r));} R[n]=q;return Q;} Top-down main function int Memoized_cut_rod (int *p,int n) {int r[11]={};for (int i=0;i<11;++i) R[i]=-1;int q=memoized_cut_rod_ Aux (p,n,r); return q;} To the top-up method to solve the steel bar problem int Bottom_up_cut_rod (int *p,int n) {int r[11]={0};for (int j=1;j<=n;++j) {int q=-1;for (int i=1;i< =j;++i) {Q=max (q,p[i]+r[p,j-i]);} R[j]=q;}  return r[n];} pair< vector<int>,vector<int> > Extened_bottom_up__cut_rot (int *p,int n) {pair< vector<int  >,vector<int> > result;  int r[11]={0};  int s[11]={0};  Result.first.push_back (0);Result.second.push_back (0);  for (int j=1;j<=n;++j) {int q=-1;  for (int i=1;i<=j;++i) {//q=-1;  Q=max (Q,p[i]+r[j-i]) if (Q<p[i]+r[j-i]) {q=p[i]+r[j-i];  S[j]=i;  }} r[j]=q;  Result.first.push_back (R[j]);  Result.second.push_back (S[j]);  } return result;    }//print result value, void print_cut_rod_solution (int *p,int n) {cout<< "Maximum return value is" <<endl;  cout<< "Steel bars are split in the way" <<endl;  pair< vector<int>,vector<int> > result;  Result=extened_bottom_up__cut_rot (P,n);  cout<<result.first[n]<<endl;  cout<< "Steel bars are split in the way" <<endl;  while (n>0) {//cout<< "steel bars are split in the form of" <<endl;  cout<<result.second[n];  N=n-result.second[n];  cout<<endl;  } cout<<endl;  cout<< "End of the split of Steel bar" <<endl; }int Main () {int Price[11]={0,1,5,8,9,10,17,17,20,24,30};//cout<<cut_rod (price,5) <<endl;cout<< Bottom_up_cut_rod (price,8) <<endl;print_cut_rod_solution (price,8); System ("pause"); return 0;}

Introduction to C + + implementation algorithm 15 chapter of the Steel bar segmentation in dynamic programming

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.