动态规划0/1背包

动态规划0/1背包

#include <iostream>
#include<algorithm>
#include<math.h>
using namespace std;
int main()
{
	int weight[] = { 1,2,3,4,1,2 };
	int value[] = { 1,3,2,5,4,2 };
	int pack = 8;
	int data[7][9];
	for (int i = 0; i < 7; i++)
	{
		data[i][0] = 0;
	}
	for (int j = 0; j <9;j++)
	{
		data[0][j] = 0;
	}
	for (int i = 1; i< 7; i++)
	{
		for (int j = 1; j <=pack;j++)
		{
			if (weight[i-1] > j)
			{
				data[i][j] = data[i - 1][j];
			}
			else
			{
				data[i][j] = max(data[i - 1][j], data[i - 1][j - weight[i - 1]] + value[i-1]);
			}
		}
	}
	for (int i = 0; i < 7; i++)
	{
		for (int j = 0; j < 9;j++)
		{
			cout << data[i][j]<<" ";
		}
		cout << endl;
	}
}
已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 数字20 设计师:CSDN官方博客 返回首页