sinat_41146801
sinat_41146801
2017-11-23 05:58

0-1背包最优解怎么去构造

  • c

#include
#define N 5
int w[N]={18,13,15,5,8};
int v[N]={20,13,26,20,70};
int p[N]={-1};
int k=0;
int tb()
{
int i;
for(i=0;i printf("% d",p[i]);
printf("\n");
}
int m (int i,int j)
{
int n=N,s1,s2;
int a=0,b=0;
if(i==n)
if(j>=w[n])
{
p[k++]=i;
return v[n];
}

else return 0;
if(j<w[i])
   return m(i+1,j);
   else
   {
     //p[k++]=i;
        s1=m(i+1,j);
        s2=m(i+1,j-w[i])+v[i];
   }

if(s1>s2)
{
       p[k++]=i;
     return s1;
}

else
{
     p[k++]=i;
      return s2;
}

}
int main()
{
int i,t,C=30;
int k=0;
t=m(0,C);
printf("%d\n",t);
tb();

}

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

1条回答

为你推荐

换一换