-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKnapsack.cpp
More file actions
47 lines (45 loc) · 732 Bytes
/
Knapsack.cpp
File metadata and controls
47 lines (45 loc) · 732 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include<iostream>
using namespace std;
int max(int,int);
int main()
{
int n,i,j,m,c;
cout<<"Enter the number of items:";
cin>>n;
cout<<"\nEnter the maximum capacity of knapsack:";
cin>>c;
int v[n+1],w[n+1];
cout<<"\nEnter the weights of items:";
for(i=1;i<=n;i++)
cin>>w[i];
cout<<"\nEnter the value of items:";
for(i=1;i<=n;i++)
cin>>v[i];
int a[n+1][c+1];
for(i=0;i<=n;i++)
{
for(j=0;j<=c;j++)
{
if(i==0 || j==0)
a[i][j]=0;
else
a[i][j]=-1;
}
}
for(i=1;i<=n;i++)
{
for(j=1;j<=c;j++)
{
if(w[i]>j)
a[i][j]=a[i-1][j];
else
a[i][j]=max(a[i-1][j],a[i-1][j-w[i]]+v[i]);
}
}
cout<<"\nMaximum profit="<<a[n][c];
return 0;
}
int max(int a,int b)
{
return a>b?a:b;
}