背包问题
给定一个容积为c的背包,去尝试装n个重量为wi、价值为vi的物体,求能装下的物体的最大价值。
#include<stdio.h>
#include<math.h>#define n 5
int w[n]={12,2,4,1,2};
int v[n]={4,2,10,1,2};
int main()
{
int i,j;
int tempvalue,tempweight;
int temp,maxvalue=0;
for (i=0;i<pow(2,n);i++)
{
temp=i;
tempvalue=tempweight=0;
for (j=0;j<5;j++)
{
if (temp%2) {tempvalue+=v[j];tempweight+=w[j];}
temp/=2;
}
if (tempweight<15&&tempvalue>maxvalue) maxvalue=tempvalue;
}
printf("%d\n",maxvalue);
return 0;
}