-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
56 lines (39 loc) · 1.2 KB
/
Copy pathmain.cpp
File metadata and controls
56 lines (39 loc) · 1.2 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
#include <iostream>
//
using namespace std;
int max(int a, int b);
int knapsack(int Bag_weith, int weith[], int values[],int n);
int main()
{
int values[] = { 60, 100, 120 }; // la valeur des objects a ajouter
int weiths[] = { 10, 20, 30 }; // le poids des objects a ajouter
int W = 50; // la poid du sac
int n = (sizeof(values)/sizeof(*values));
cout << knapsack(W, weiths, values, n) << endl;
return 0;
}
int max(int a, int b){
/* params: a integer value
params: b integer value
return the max between a and b
*/
return (a>b) ? a:b;
}
int knapsack(int bag_weith, int weith[], int values[],int n){
int bag_profit [n+1][bag_weith+1];
for(int i = 0; i<=n; i++ ){
for (int w=0; w<=bag_weith; w++){
if (i == 0 || w == 0) {
bag_profit[i][w] = 0;
}
else if(weith[i-1] <= w){
bag_profit[i][w] = max(
values[i-1] + bag_profit[i-1][w - weith[i-1]], bag_profit[i-1][w]);
}
else{
bag_profit[i][w] = bag_profit[i-1][w];
}
}
}
return bag_profit[n][bag_weith];
}