描述:
给定一段长度为n英寸的钢条和一个价格表 (i=1,2, …,n),求切割钢条的方案,使得销售收益 最大。注意,如果长度为n英寸的钢条价格足够大,最饥饿可能就是完全不需要切割。
若钢条的长度为i,则钢条的价格为Pi,如何对给定长度的钢条进行切割能得到最大收益?
长度i 1 2 3 4 5 6 7 8 9 10
价格Pi 2 , 4, 8 , 9 , 10 , 17 , 17 , 20, 24 , 30
注意:因为测试需要,完整版的单价我已经改为以上价格,核心版本单价为算法导论课的没改变。
核心版本
#include<iostream>
#define N 10
using namespace std;
int count = 0;
//定义长度为i米的单价为a[i].最长为10米
const int p[] = {0,1,5,8,9,10,17,17,20,24,30};
/*核心算法*/ //递归求解出如何切断才能获得最高价格
int Steel_fun1(int n,int *r)
{
int q;
//检查备忘录中,是否之前已经求过,如果求过,则返回其最大利益。
if(r[n]>=0)
return r[n];
if(n==0) q = 0; //递归出口
else q = -32767;
for(int j = 1; j<=n ; j++)//求出第i段的最大收益
q = max(q,p[j]+Steel_fun1(n-j,r));
r[n] = q; //保存最大收益
return q;
}
int Steel_fun(int n)
{
int *r = new int[N+1];
for(int i = 0 ; i<=N; i++)
r[i] = -32767;
int max;
if(n>10)
{
Steel_fun1(N,r);
int count = n/N;
max = count*r[10];
max +=Steel_fun1(n%N,r);
}
else
max =Steel_fun1(n%N,r);
delete(r);
return max;
}
int main(void)
{
int liyi = Steel_fun(18);
cout<<"最大收益="<<liyi<<endl;
return 0;
}
完整版
#include<iostream>
#define N 10
using namespace std;
//定义长度为i米的单价为a[i].最长为10米
const int p[] = {0,2,4,8,9,10,17,17,20,24,30};
//定义结构体,用来保存 切割方案 和 最大利益
typedef struct Cut
{
int duan_dian;//保存切割断点
int money; //保存切割方案获得的收益
}Steel;
void print(Steel *,int ,int ,int);
/*核心算法*/ //递归求解出如何切断才能获得最高价格
int Steel_fun1(int n,Steel *r)
{
int q;
//检查备忘录中,是否之前已经求过,如果求过,则返回其最大利益。
if(r[n].money>=0)
return r[n].money;
if(n==0) q = 0; //递归出口
else q = -32767;
int a=n,temp = -32767; //用a来保存切断位置 temp来保存切断结果
for(int j = 1; j<=n ; j++)
{
temp = p[j]+Steel_fun1(n-j,r);
if(q<temp)
{
q = temp;
a = j;
}
}
if(a==n) r[n].duan_dian = n; //当未切断时,duan_dian保存本身
else r[n].duan_dian = a; //切断后,断点保存为切断长度
r[n].money = q; //money保存最大收益
return q;
}
int Steel_fun(int n)
{
int max = -32767;
int temp_N = N;
int temp_n = n;
int count = 0;
Steel *r = new Steel[N+1];
for(int i = 0 ; i<=N; i++)
r[i].money = -32767;
/*为格式化输出做准备*/
if(n<=N)
max = Steel_fun1(n,r);//如果要求的范围在10米之内,不用格式化
else
{
Steel_fun1(N,r);
while(r[temp_N].duan_dian!=temp_N)
temp_N--;
count = n/temp_N;
temp_n = n%temp_N;
max = Steel_fun1(temp_n,r);
max +=count*r[temp_N].money;
}
print(r,temp_N,count,temp_n);//格式化输入
delete(r);
return max;
}
//格式化输入代码段
void print(Steel *r,int temp_N,int count,int n)
{
int temp_n=n;
cout<<"长度为i米的单价为p[i].最长为10米,单价如下:\n";
for(int i = 0 ;i<sizeof(p)/sizeof(int); i++)
{
cout<<"p["<<i<<"]="<<p[i]<<"";
if(i==5)
cout<<"\n";
}
cout<<"\n\n以下是长度为i,最高收益为r[i]\n";
int i = 0;
while(r[i].money>=0&&i<=N)
{
cout<<"r["<<i<<"].money="<<r[i].money<<endl;
i++;
}
cout<<"切割方案为: ";
if(count>0)
cout<<count<<" X"<<temp_N<<"米," ;
while((r[n].duan_dian!=n)&&(n-r[n].duan_dian!=r[n].duan_dian))
{
cout<<r[n].duan_dian<<"米,";
if(r[n].duan_dian!=n)
n=n-r[n].duan_dian;
else
n = r[n].duan_dian;
}
if(temp_n==n&&count==0)
cout<<"无切割方案"<<endl;
else
cout<<n<<"米。"<<endl;
}
int main(void)
{
int n = 118;
int liyi = Steel_fun(n);
cout<<"长度="<<n<<"米\n最大收益="<<liyi<<"元"<<endl;
return 0;
}
参考网站:http://blog.youkuaiyun.com/allen_fan_01/article/details/9006809 http://blog.youkuaiyun.com/xlf13872135090/article/details/17377697