-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathknapsack.cpp
More file actions
67 lines (59 loc) · 1.13 KB
/
knapsack.cpp
File metadata and controls
67 lines (59 loc) · 1.13 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include<iostream>
using namespace std;
int t[52][52];
int max(int x , int y)
{
if(x>y)
return x;
else
return y;
}
int knapsack(int wt[] , int val[], int w , int n)
{
if(n==0 || w==0)
return 0;
if(t[n][w] != -1)
return t[n][w];
if(wt[n-1] <= w)
return t[n][w]=max(val[n-1] + knapsack(wt, val , w-wt[n-1] , n-1) , knapsack(wt, val, w , n-1));
else if(wt[n-1] > w)
return t[n][w]=knapsack(wt, val , w, n-1);
}
int main()
{
for(int i=0; i<50; i++)
{
for(int j=0; j<50; j++)
{
t[i][j]=-1;
}
}
int n;
cout << "size of array : " << endl;
cin >> n;
int wt[n], val[n] , w ;
cout << "enter the weight array : " << endl;
for(int i=0; i<n; i++)
{
cin >> wt[i];
}
cout << " enter the value array : " << endl;
for(int i=0; i<n; i++)
{
cin >> val[i];
}
cout << " enter the total weight of knapsack : " << endl;
cin >> w;
int res= knapsack( wt , val , w , n);
cout << " the result is : " << res << endl;
cout << endl;
for(int i=0; i<50; i++)
{
for(int j=0; j<50; j++)
{
cout << t[i][j] << " ";
}
cout << endl ;
}
return 0;
}