0-1背包动态规划c语言,动态规划解决0-1背包问题程序看不懂,请大家看看帮忙解决下...
該樓層疑似違規已被系統折疊?隱藏此樓查看此樓
c-=w[i];
}
}
if(m[n][c] >0)
flag[n] = 1;
else
flag[n] = 0;
}
/* 打印最優解*/
void printResult(int flag[NUM],int w[NUM],int v[NUM],int m[NUM][content])
{
int i;
printf("the knapsack should contain:\n");
printf(" num weight value \n");
for(i = 0;i < NUM; i++)
{
if(flag[i] == 1)
printf("?? %d???? %d????? %d\n",i,w[i],v[i]);
}
printf("the max value in the knapsack is: %d\n",m[0][content]);
printf("the real weight of the knapsack is: %d\n",m[0][content]);
}
int main()
{??? int i;
int value[NUM];
int weight[NUM];
int c = content;
int maxvalue[NUM][content];
int flag[NUM]={0,0,0,0,0,0,0,0,0,0};
system("cls");
printf("****************************************\n");
printf("*??????????????? welcome???????????????? *\n");
printf("*?????? this program will solve????????? *\n");
printf("*?????? the problem of knapsack????????? *\n");
printf("****************************************\n");
printf("please input the value of every one:\n");
for(i = 0;i < NUM; i++)
scanf("%d",&value[i]);
printf("please input the weight of every one:\n");
for(i = 0;i < NUM; i++)
scanf("%d",&weight[i]);
/*計算最優值*/
knapsack(value,weight,c,maxvalue);
/*構造最優解*/
traceback(flag,weight,maxvalue);
/*打印程序的結果*/
printResult(flag,weight,value,maxvalue);
getch();
return 0;
}
總結
以上是生活随笔為你收集整理的0-1背包动态规划c语言,动态规划解决0-1背包问题程序看不懂,请大家看看帮忙解决下...的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: 微信怎么找不到银乐融
- 下一篇: c语言中的fock方法输出hello,涉
