数据水了! 把楼上的代码改了一下,当h为0的时候,也按背包的方法把第k件物品装上去。

#include<bits/stdc++.h>
using namespace std;
const int N = 110;
int m, n, h;
int w[N], v[N];
int f[N][N * 2];// 0<=n,m,h<=100
int main() 
{
	while (cin >> n) 
	{
		if (n == 0) break;
		cin >> m >> h;
		for (int i = 1; i <= n; i++) cin >> v[i] >> w[i];
		int ans = 0;
		for (int k = 1; k <= n; k++) 
		{
			memset(f, 0, sizeof(f));
			for (int i = 1; i <= n; i++) 
			{
				if (i == k) 
				{
					for (int j = 0; j <= m + h; j++)
						f[i][j] = f[i - 1][j];
					continue;
				}
				for (int j = 0; j <= m + h; j ++) 
				{
					f[i][j] = f[i - 1][j];
					if (j >= v[i]) f[i][j] = max(f[i][j], f[i - 1][j - v[i]] + w[i]);
				}
			}
			if (h > 0) ans = max(ans, f[n][m + h - 1] + w[k]);
			else
            {//修改的部分 
                for(int j = 0; j <= m + h; j ++)
                {
                    if(j >= v[k]) ans = max(max(ans, f[n][j - v[k]] + w[k]), f[n][j]);
                }
            }
		}
		cout << ans << endl;
	}
	return 0;
}