多重背包Marsha and Bill own a collection of marbles.

死磕很久的石头题,目前还没有找到二进制优化。 

#include 
#include
using namespace std;
int dp[10005];
int val[7] = { 0,1,2,3,4,5,6 };//每件的价值
int num[7];//数量
int main()
{
	int n = 1;
	while (1)
	{
		memset(dp, 0, sizeof(dp));
		memset(num, 0, sizeof(num));
		int sum = 0; int equ;
		for (int i = 1; i <= 6; i++)
		{
			cin >> num[i];
			sum = sum + num[i] * val[i];
		}
		if (sum == 0)
			break;
		if (sum % 2 != 0)
		{
			cout << "Collection #" << n << ":" << endl;
			cout << "Can't be divided." << endl;
			cout << endl;
			n++;
		}
		else
		{
			equ = sum / 2;
			for (int i = 1; i <=6; i++)
			{
				for (int k = 1; k <=num[i]; k++)
				{
					for(int j=equ;j>=val[i];j--)
						dp[j] = max(dp[j], dp[j - val[i]] + val[i]);

				}
			}
			if (dp[equ] == equ)
			{
				cout << "Collection #"<

 

你可能感兴趣的:(code)