# 01 knapsack problems-to be continued --

Source: Internet
Author: User
`# Include <stdio. h> # Define Maxcompute 100 //  Maximum number of types  Int  Current [Max];  Int  OPT [Max];  Int  T;  Double  Totalv;  Double  Totalw;  Double  Maxvalue;  Struct {  Double  Value;  Double  Weight;} goods [Max];  Void Checkout ( Int I, Double Tw, Int  Totalvalue );  Void  Main (){  Int  I;  Double Value, weight; printf (  "  Enter the number of categories  "  ); Scanf (  "  % D  " ,& T); printf (  "  Enter the price and weight of the product:  "  );  For (I = 0 ; I <t; I ++ ) {Scanf ( "  % Lf  " , & Value ,& Weight); goods [I]. Value = Value; goods [I]. Weight = Weight; totalv + = Value;} printf (  "  Enter weight limit  "  ); Scanf (  "  % Lf  " ,&Totalw); maxvalue = 0.0  ;  For (I = 0 ; I <t; ++ I) Current [I] = 0  ; Checkout (  0 , 0.0  , Totalv );  For (I = 0 ; I <t; ++ I)  If (OPT [I]) printf (  "  % D \ t  " , I + 1  ); Printf (  "  Total value: % d  "  , Maxvalue );}  Void Checkout ( Int I, Double Tw, Int  Totalvalue ){  Int K;  If (TW + goods [I]. weight <= Totalw) {current [I] = 1  ;  If (I <t- 1  ) {Checkout (I + 1  , TW, totalvalue );}  Else  {  For (K = 0 ; K <t; ++K) {OPT [k] = Current [k];} maxvalue = Totalvalue;} current [I] = 0  ;  If (Totalvalue-goods [I]. value> Maxvalue ){  If (I <t- 1  ) {Checkout (I + 1 , TW, totalvalue- Goods [I]. Value );} Else  {  For (K = 0 ; K <t; ++ K) {OPT [k] = Current [k];} maxvalue = Totalvalue- Goods [I]. Value ;}}} `
Related Keywords:

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.