#include<iostream> using namespace std; int bottom_up_cut_rod(int p[],int n,int &pos) { int *r=new int[n+1]; int *s=new int[n+1]; for(int i=0;i<=n;++i) s[i]=0; for(int i=0;i<=n;++i) r[i]=0; for(int j=1;j<=n;++j) { int q=-1; for(int i=1;i<=j;++i) { //q=q>p[i]+r[j-i]?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; } pos=s[n]; return r[n]; } int main() { const int size=10 ; int p[11]={0,1,5,8,9,10,17,17,20,24,30}; int pos; int sum=bottom_up_cut_rod(p,size,pos); cout<<sum<<endl; cout<<pos<<endl; system("pause"); return 0; }
时间: 2024-11-25 06:08:13