Use back to solve the backpack problem

xiaoxiao2021-03-05  29

/ * Compilation Environment: GCC (GCC) 3.2 20020903 (Red Hat Linux 8.0 3.2-7) Copyright (c) 2002 Free Software Foundation, Inc.author: Ningoo*/#include #define n 6

INT main () {// Select the backpack of T on the N backpack (W [k] per backpack), how many of the number of option INT w [n] = {1, 8, 3, 4, 5, 2}; // 6 backpack INT T = 10; // Total INT K = 0; INT i = 0; INT J = 1; struct stacks {// stack int S [n]; int top;} THE_STACK;

// Initialization stack for (i = 0; i

Do {While (T> 0 && K <= n) {f (t> = w [k]) {// Equity Conditional Backpack Inn THE_STACK.S [THE_STACK.TOP ] = K; T- = W [k]; } K ; // Do not meet the next backpack} if (t == 0) {// find a method, output printf ("---------- Answer% d --- --------- / n ", j); for (i = 0; I

T = W [k]; k ;} while (! (The_stack.top == 0 && k == n))); // When the stack is empty and k == n, all possible combinations are completed, and the loop} is launched RESULTS - ------------ Answer1 ------------ 0 [1] 2 [3] 3 [4] 5 [2] ------- ----- Answer2 ------------ 0 [1] 3 [4] 4 [5] ------------ Answer3 ------- ----- 1 [8] 5 [2] ------------ Answer4 ------------ 2 [3] 4 [5] 5 [2]

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

New Post(0)