
We are considering that we have to take full item (not its some part) : (0/1 knapsack )
Greedy approach to solve this is :
Consider all subsets of the items and calculate the total weight and value of all subsets. We will consider only those subsets whose total weight is smaller than W. From all such subsets, pick the maximum value subset.
Algorithm :
Input : W is max weight possible
item[] : It is an structure array . It has two parts :
item.v = it's value ; item.w = it's weight
1. Get the max no. of subsets possible
max_no_of_subsets = pow (2 , no_of_items)
2. Assign the result to zero
result = 0
3. Loop for counter from 0 to max_no_of_subsets
a) Assign the temporary variable to use them
present_weight = 0;
present_value = 0
(b) Loop for i = 0 to no_of_items
i) if ith bit is set in counter
present_weight = present_weight + item[i].w;
present_value = present_value + item[i].v;
(c) If present_weight is less than max_weight_possible, then update result i.e.
if (( present_weight <= W ) && (result < current_result))
result = current_result
4. return the value of result // It is the answer
-------------------------------------------------------------------
Solution to your problem :
We have 5 items . So max no. of subsets = 32;
Answer for your problem is : 21
------------------------
Code :
---------------
#include <iostream>
using namespace std;
struct item
{
int v;
int w;
};
int main() {
// your code goes here
item data[5];
int tot_weight = 600;
// Enter the data
for(int i=0; i<5; i++)
cin>>data[i].v;
for(int i=0; i<5; i++)
cin>>data[i].w;
int res = 0;
for(int i=0; i<32; i++) // Max no. of subset = 32
{
int temp_weight = 0; int temp_val = 0;
for(int j =0; j<5; j++)
{
if(i & (1<<j)) // If ith bit is set
{
temp_weight += data[j].w;
temp_val += data[j].v;
}
}
if((temp_weight < tot_weight) && (temp_val > res)) // update the result
res = temp_val;
}
cout<<res;
return 0;
}
-----------------------------------Screenshot -------------------------

If any doubt ... please ask in comment section i wiil clear it
1. Draw the orbital diagram of benzene Please help this is due today your help is greatly appreciated thank you.
I need help with my homework that I need help with please it is due today I need help with it please help me and be clear copy and paste your answers please do not write anything that is not clear just copy and paste it on with good summary on reach resource I have annotated bibliography that I need help with need all resources regarding nonprescription drug permits please be clear and correct Provide 5 citations for references and ....
I need help with my homework that I need help with please it is due today I need help with it please help me and be clear copy and paste your answers please do not write anything that is not clear just copy and paste it on 1-8, please I have annotated bibliography that I need help with need all resources regarding nonprescription drug permits For this week's assignment, you will need to create an annotated bibliography with a minimum...
Please help me with this question today is my due date Describe a real world example that uses one of the Data Mining Tasks and why is this task best suited to this example?
its due today and i want to be completw plwase help me
I'll appreciate it for all the Help
Find the surface area of each cone below. 7. 11.4 m - 7 m 8. 27 mm 32.9 mm 9. 27.3 in 25.2 in
Please Help! Due tomorrow!
ment stream consists of three payments:$1000 due today, $1500 due 70, days from A pay today, and $2000 due 210 days from today what single payment, G0 days from today, is economi ically equivalent to the payment stream if money can be invested at a rate of З 5%? Payments of $1300 due five months ago and $1800 due three months from Now are to be replaced by a single payment at a focal date one month from Now. what...
Please show work
10. 8 If money is worth j 8%, what single replace $1000 due today plus a $2000 debt due at the end of 4 years with interest at 123% per annum compounded semi-annually? sum of money payable at the end of 2 years will equitably
10. 8 If money is worth j 8%, what single replace $1000 due today plus a $2000 debt due at the end of 4 years with interest at 123% per annum compounded...
Please help How communication with older alduts apply to nursing today and why it is important
Please help How communication with older alduts apply to nursing today? why it is important?