Ga.难搞啊 提问:英雄歌,这个也是照搬的,一一对比,但是答案是37,输出却是36,这是为何
#include<iostream>
using namespace std;
#define maxn 101
#define maxv 1001
#define init 0
#define inf -1
#define vType int
vType opt(vType a,vType b){
if(a == inf) return b;
if(b == inf) return a;
return a > b ? a : b;
}
void Knapsack01(int n,int V,int w[maxn],vType v[maxn],vType dp[maxv]){
for(int i = 1;i <= V;i++){
dp[i] = inf;
}
dp[0] = 0;
//状态转移
for(int i = 1;i <= n;++i){
for(int j = V;j >= w[j];--j){
dp[j] = opt(dp[j],dp[j-w[i]]+v[i]);
}
}
}
//定义全局变量
int w[maxn];
vType v[maxn];
vType dp[maxv];
int main(){
int n,V;
cin >> n >>V;
for(int i = 1;i <= n;++i){
cin >> w[i] >> v[i];
}
Knapsack01(n,V,w,v,dp);
vType ret = 0;
for(int i = 0;i<= V;++i){
ret = opt(ret,dp[i]);
}
cout << ret;
}
输出的时候少了个 换行:cout...