标签:style color os io 数据 for ar div
ctest有n个苹果,要将它放入容量为v的背包。给出第i个苹果的大小和价钱,求出能放入背包的苹果的总价钱最大值。
3 3 1 1 2 1 3 1 0 0
2
知识点:01背包、动态规划
难点:动态方程的书写
#include<cstdio>
#include<cstring>
int main()
{
int a,b,dp[1100],i,j,v,n;
while(scanf("%d%d",&n,&v),!(n==0&&v==0))
{
memset(dp,0,4400);
for(i=1;i<=n;i++)
{
scanf("%d%d",&a,&b);
for(j=v;j>=a;j--)
if(dp[j]<dp[j-a]+b)
dp[j]=dp[j-a]+b;
}
printf("%d\n",dp[v]);
}return 0;
}
标签:style color os io 数据 for ar div
原文地址:http://blog.csdn.net/hpuhjl/article/details/38345443