算法导论例题——钢管切割

《算法导论》(CLRS)中第一个dp例题(rodcutting)的C++代码

#include
#include
#include
using namespace std;
int main()
{
	int price[11] = { 0,1,5,8,9,10,17,17,20,24,30 };
	int n;
	cin >> n;
	vector table(n+1,0);
	for (int i = 1; i <= n; i++)
	{
		int max = -1;
		for (int j = 1; j <= i; j++)
		{
			if (price[j] + table[i - j] >= max)
			{
				max = price[j] + table[i - j];
			}
		}
		table[i] = max;
	}
	for (int i = 1; i <= n; i++)
	{
		cout << i<<" "<






你可能感兴趣的:(算法导论例题——钢管切割)