14. Knapsack problem [Recursive implementation]

Source: Internet
Author: User

/*------------Full code @ Reflection Snow-------------*//*Initializes a set of data, saves input, and facilitates commissioning*/#include<iostream>using namespaceStd;typedefstruct{    intvalue[5];/*Value Table*/    intweight[5];/*Weight Table*/       intNum/*Quantity*/    intLIMITW;/*Maximum weight*/    int Select[5];} Goods;intMaxvalue=0, totalvalue= the;//maximum value of the project, total value of the itemintselect1[5]={0};/*Global*/voidBackpack (Goods &g,intIinttwintTv//parameters for item I, current selection already achieved weight and TW, the total value that this scenario may achieve{   intK; if(TW + g.weight[i] <= G.LIMITW)//The item I is included in the current scheme with a weight less than or equal to the weight limit{Select1[i]=1;//Select Article I items      if(I < g.num-1)//If Item I is not the last itemBackpack (g, i +1, TW + g.weight[i], TV);//recursive call, continue to add the next item      Else //if the last item has been      {          for(k =0; K < G.num; ++K)//Copy the status flag into the option arrayG.Select[K] =Select1[k]; MaxValue= TV;//Save the maximum value of the current scenario}} Select1[i]=0;//Cancel Item I selection status   if(Tv-g.value[i] > MaxValue)//if the total value of the item minus the value of item I is greater than the value already in the MAXV scheme, you can also continue to add items to the program   {      if(I < g.num-1)//If Item I is not the last itemBackpack (g, i +1, TW, tv-g.value[i]);//recursive call, continue to add the next item      Else //if the last item has been      {            for(k =0; K < G.num; ++K)//Copy the status flag into the option array{G.Select[K] =Select1[k]; } MaxValue= Tv-g.value[i];//Save the maximum value of the current scheme (subtract the value of item I from the total value of the item)      }   }}intMain () {intsumweight=0; Goods g={{ +, A,7,8, -},{5,2,1,1,6},5,8,{0}};/*initialize a set of data*/cout<<"the maximum weight of the backpack can be:"<<g.limitw<<Endl;  for(inti =0; i < G.num; i++) cout<<"Section"<<i+1<<"number of items heavy"<<g.weight[i]<<" "<<"values are:"<<g.value[i]<<Endl; Backpack (g,0,0, Totalvalue); cout<<"The result of the calculation is:"<<Endl;  for(i =0; i < G.num; ++i)if(g.Select[i]) {cout<<"Section"<<i+1<<"number of items heavy"<<g.weight[i]<<" "<<"values are:"<<g.value[i]<<Endl; Sumweight+=G.weight[i]; } cout<<"The total weight is:"<<sumweight<<"The total value is:"<<maxvalue<<Endl; return 0;}

14. Knapsack problem [Recursive implementation]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.