Program to help select the treasures so that the total value of the selected treasures is maximized
Input:
weight_that_you_can_carry
number_of_items
weight value
weight value
weight value
weight value
...
Output:
maximum_total_value
weight value (item no.)
weight value (item no.)
....
Sample input 1:
30
7
28 70
20 5
17 35
15 30
5 15
3 20
10 30
Sample output 1:
85
17 35 (item 2)
3 20 (item 5)
10 30 (item 6)
Each item can be either taken or not taken (but not partially taken)
You should first write a program to compute the maximum value that can be taken away before enumerating each individual item.
int maxvalue(int W[], int V[], int totalweight, int beg, int end, int flag[])
int flag[] is set in the subroutine,
Inside this routine, you needs to declare two flag arrays for the two choices
You should then copy and return the right flag array and set the right flag
value for the choice your make for the first item.
----------
these are the guidelines, but i have no idea where to go from here.
the outline is as follows, i just can't figure out how to go about implementing it. i've been racking my brain for 2 days
1 2 3 4 5 6 7 8
|
int maxvalue(int W[], int V[], int totalweight, int beg, int end, int flag[]) {
int flag_for_choice1[42];
int flag_for_choice2[42];
….
…. maxvalue(W, V, totalweight-W[beg], beg+1, end, flag_for_choice1)
….
// copy one of flag_for_choice to flag
}
|
thx in advance for any help