-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathknapsack_memoization.cpp
More file actions
58 lines (49 loc) · 1.26 KB
/
knapsack_memoization.cpp
File metadata and controls
58 lines (49 loc) · 1.26 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
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
int t[10][10];
int knapsack(int wt[] , int value[] , int w , int n){
if(n == 0 || w == 0){
return 0;
}
else if(t[n][w] != -1){
return t[n][w];
}
else if(wt[n-1]<=w){
return t[n][w] = max(value[n-1] + knapsack(wt , value , w - wt[n-1] , n-1) , knapsack(wt , value ,w , n-1));
}
else{
return t[n][w] = knapsack(wt , value ,w , n-1);
}
}
int main(){
memset(t , -1 , sizeof(t));
int n , m , size , capacity;
cout<<"Enter How many element in weight array and value array : "<<'\n';
cin>>n;
int wt[n] , value[n];
for(int i = 0; i<n; i++){
cout<<"Enter a "<<i+1<<" element of weight array : "<<'\t';
cin>>wt[i];
cout<<'\n';
cout<<"Enter a "<<i+1<<" elemet of value array : "<<'\t';
cin>>value[i];
cout<<'\n';
}
cout<<"Enter a capacity of knapsack : "<<'\n';
cin>>capacity;
int result = knapsack(wt , value , capacity , n);
cout<<"weight array : "<<'\t';
for(int i = 0; i<n; i++){
cout<<wt[i]<<'\t';
}
cout<<'\n';
cout<<"value array : "<<'\t';
for(int i = 0; i<n; i++){
cout<<value[i]<<'\t';
}
cout<<'\n';
cout<<"Capacity of knapsack : "<<capacity<<'\n';
cout<<"You got max proft of : "<<result;
}