Backpack problem

xiaoxiao2021-03-06  51

The backpack can only load S weight, there is n objects, the weight is W1, W2 ... Wn, and if you want to take the next to S, ask how to use the recursive method, language is not limited. C language implementation / * Recursive algorithm for simplified backpack issues * /

#include #include

INT KNAP (INT S, INT N, INT W []) {IF (S == 0) Return (1); ELSE IF (S <0 || S> 0 && n <1) Return (0); Else IF (KNAP (S - W [N-1], N - 1, W) == 1) {Printf ("Result: n =% D, W [% D] =% D / N", N, N-1 , W [N-1]); RETURN (1);} else return (KNAP (S, N - 1, W));}

INT main () {int * W; int S = 0, n = 0, Result = 0, i = 0; Printf ("please input s ="); / * Enter S * / scanf ("% d", & s ); Printf ("please input n ="); / * Enter n * / scanf ("% D", & n); w = (int *) malloc (n * sizeof (int)); Printf ("please input the % D Numbers (Weight): / N "; / * Enter Weight * / for (i = 0; I

Public class kao {public boolean Digui (Double S, Double [] W, Vector Retvec) {INT I, J; Double DBLSUM = 0; // Put the total weight boolean blnfind = false; // Find flag Double [] Subw = null; int nowSize = retvec.size (); // Calculated the weight for the package already put into the package for (i = 0; i

/ ** * Part: *

description: *

Copyright: CopyRight (C) 2004 *

Company: * @Author not attributable * @version 1.0 * / class thing {private int myweight;

Public Thing (INT Weight) {MyWeight = Weight;

Public int getWeight () {return myweight;

Public void setWeight (INT weight) {myweight = weight;} public string toString () {return integer.tostring (myweight);}} / ** * Package *

title: *

Description: *

Copyright: CopyRight (c) 2004 *

Company: * @Author not attributable * @version 1.0 * / class bag {private list mythings = new ArrayList (); private int allweight = 0; private int exciteun = 0;

Public Bag (INT CONT) {RETURN Mythings;} public boolean putthings (thing []o) {return putthings (things, 0, 0);}

/ ** * recursive function specified object placement * @param things Thing [] * @param previousCont int * @param curNum int * @return boolean * / private boolean putThings (Thing [] things, int previousCont, int curNum) { IF (things == null) {Return False;}

IF (PREVIOUSCONT

转载请注明原文地址:https://www.9cbs.com/read-83120.html

New Post(0)