五个物品的重量:2,2,6,5,4
每个物品的价值:6,3,5,4,6
#include<stdio.h>
int main (){
int C,n,j,i ;
int x[20];
int v[20][20];
int w[6]= {0,2,2,6,5,4};
int a[6]= {0,6,3,5,4,6};
C=10;
n=5;
j=C;
for (i=0; i<=n; i++) v[i][0]=0;
for (j=0; j<=C; j++) v[0][j]=0;
for(i=1; i<=n; i++)
for(j=1; j<=C; j++){
if(j<w[i])
v[i][j]=v[i-1][j];
else
v[i][j]=(v[i-1][j]>=v[i-1][j-w[i]]+a[i])?v[i-1][j]:v[i-1][j-w[i]]+a[i];
}
j=C;
for(i=n; i>0; i--)
{
if(v[i][j]>v[i-1][j])
{
x[i]=1;
j=j-w[i];
}
else x[i]=0;
}
//printf("%d\n",v[n][C]);
for(i=1; i<=n; i++) printf("%d\t",x[i]);
return 0;
}