钢管切割 2018-2-5

     钢管切割原始问题:


        某公司生产长钢管,然后一般,会将钢条切断,变成不同长度,然后去售卖。其中有个问题是,不同长度的钢管的售价是不一样的,但是它们并不是完全按照比例来,比如2米的钢管售价要比3米的钢管售价要少,但是并不是2比3的比例。钢管的长度售价表如下:


 长度i1      2      3      4      5      6      7      8      9      10
价格Pi1      5      8      9     10    17    17     20    24    30



        于是问题就来了,比如30米长的钢管,要如何切割,切割成多长的几条,才能让售价最高,收益最高呢?

using namespace std;


struct result {
	int* r;
	int* s;
	int len;
	result(int l) :
			r(), s(), len(l) {
		r = new int[len];
		s = new int[len];
		r[0] = 0;
	}

	~result() {
		delete[] r;
		delete[] s;
	}
};

result* extended_bottom_up_cut_rod(int p[], int n) {
	result* res = new result(n + 1);
	int q = -1;

	//外层的循环代表的是保留的不切割的那段
	for (int i = 1; i <= n; i++) {

		//内层的循环代表的是要分割的,且要求出最佳分割的那段
		for (int j = 1; j <= i; j++) {
			if (q < p[j] + res->r[i - j]) {
				q = p[j] + res->r[i - j];
				res->s[i] = j;
			}
		}
		res->r[i] = q;
	}
	return res;
}

int main() {
	int p[] = { 0, 1, 5, 8, 9, 10, 17, 17, 20, 24, 30 };

	int n = 9;

	result* res = extended_bottom_up_cut_rod(p, n);

	cout <<res->r[9] << endl;//循环输出实际的最佳分割段长
	cout << "分段情况:";
	while (n > 0) {
		cout << res->s[n] << ' ';
		n = n - res->s[n];
	}

	delete res;

	return 0;
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值