AC代码:
#include
using namespace std;
#define _for(i,a,b) for(int i=a;i<=b;i++)
const int maxn = 20007;
int n,m,a[35],dp[maxn];
int main(int argc, char const *argv[])
{
cin>>m>>n;
_for(i,1,n)cin>>a[i];
_for(i,1,n)
{
for(int j = m;j>=a[i];j--)
{
dp[j]=max(dp[j],dp[j-a[i]]+a[i]);
}
}
cout<
AC代码:
#include
using namespace std;
#define _for(i,a,b) for(int i=a;i<=b;i++)
const int maxn = 107;
const int N = 1006;
int n,m,val[maxn],weight[maxn],dp[N];
int main(int argc, char const *argv[])
{
cin>>m>>n;
_for(i,1,n)cin>>weight[i]>>val[i];
_for(i,1,n)
{
for(int j=m;j>=weight[i];j--)
{
dp[j]=max(dp[j],dp[j-weight[i]]+val[i]);
}
}
cout<
AC代码
#include
using namespace std;
#define _for(i,a,b) for(int i=a;i<=b;i++)
const int maxn = 10007;
const int N = 100006;
int n,m,val[maxn],weight[maxn],dp[N];
int main(int argc, char const *argv[])
{
cin>>m>>n;
_for(i,1,n)cin>>weight[i]>>val[i];
_for(i,1,n)
{
_for(j,weight[i],m)
{
dp[j]=max(dp[j],dp[j-weight[i]]+val[i]);
}
}
cout<
#include
using namespace std;
const int maxn = 66;
const int N = 32006;
#define _for(i,a,b) for(int i=a;i<=b;i++)
int n,m,main_val[maxn],main_weight[maxn],dp[N],vis[maxn],val[maxn][3],weight[maxn][3];
int main(int argc, char const *argv[])
{
cin>>m>>n;
_for(i,1,n)
{
int x,y,z;
cin>>main_weight[i]>>y>>z;
if(z==0)main_val[i]=main_weight[i]*y;
else if(weight[z][0]!=0)
{
weight[z][1]=main_weight[i];
val[z][1]=main_weight[i]*y;
}
else
{
weight[z][0]=main_weight[i];
val[z][0]=main_weight[i]*y;
}
}
_for(i,1,n)
{
for(int j=m;j>=main_weight[i];j--)
{
dp[j]=max(dp[j],dp[j-main_weight[i]]+main_val[i]);
if(j>=main_weight[i]+weight[i][0])
{
dp[j]=max(dp[j],dp[j-main_weight[i]-weight[i][0]]+val[i][0]+main_val[i]);
}
if(j>=main_weight[i]+weight[i][1])
{
dp[j]=max(dp[j],dp[j-main_weight[i]-weight[i][1]]+val[i][1]+main_val[i]);
}
if(j>=main_weight[i]+weight[i][0]+weight[i][1])
{
dp[j]=max(dp[j],dp[j-main_weight[i]-weight[i][0]-weight[i][1]]+val[i][0]+main_val[i]+val[i][1]);
}
}
}
cout<
(1)if(j==第i道菜的价格)f[i][j]=f[i-1][j]+1;
(2)if(j>第i道菜的价格) f[i][j]=f[i-1][j]+f[i-1][j-第i道菜的价格];
(3)if(j<第i道菜的价格) f[i][j]=f[i-1][j];
AC代码:
/*
* @Author: 王文宇
* @Date: 2018-03-02 14:38:17
* @Last Modified by: 王文宇
* @Last Modified time: 2018-03-06 12:57:37
*/
#include
using namespace std;
const int maxn = 107;
const int N = 10006;
#define _for(i,a,b) for(int i=a;i<=b;i++)
int n,m,dp[maxn][N],a[maxn];
int main(int argc, char const *argv[])
{
cin>>n>>m;
_for(i,1,n)cin>>a[i];
_for(i,1,n)
{
_for(j,1,m)
{
if(a[i]==j)dp[i][j]=dp[i-1][j]+1;
else if(a[i]>j)dp[i][j]=dp[i-1][j];
else dp[i][j]=dp[i-1][j]+dp[i-1][j-a[i]];
}
}
cout<