-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
44 lines (43 loc) · 1.03 KB
/
main.cpp
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
#include <iostream>
using namespace std;
void knapsack(int* X,int* optX,int size,int* w,int* p,int b,int* optP,int l=0){
if(l == size){
int sum=0;
for(int i=0;i<l;i++){
if(X[i])
sum+=w[i];
}
if(sum <= b){
sum=0;
for(int i=0;i<size;i++){
if(X[i])
sum+=p[i];
}
if(sum > *optP){
*optP = sum;
for(int i=0;i<size;i++)
optX[i] = X[i];
}
}
}
else{
X[l] = 1;
knapsack(X,optX,size,w,p,b,optP,l+1);
X[l] = 0;
knapsack(X,optX,size,w,p,b,optP,l+1);
}
}
int main(){
const int size = 4;
int X[size];
int optX[size]={0};
int w[size] = {10,20,15,12};
int p[size] = {6,9,8,7};
int b = 27;
int optP = 0;
knapsack(X,optX,size,w,p,b,&optP);
for(int i=0;i<size;i++)
cout<<optX[i]<<' ';
cout<<endl;
return 0;
}