#pragma once
#include <stdio.h>
int len = 10;
int w[] = { 1,5,7,9, 9, 1,6, 3,8,10 };
int v[] = { 2,7,5,8,11,62,9,10,1,100 };
int tmp[10] ;
int result[10] ;
int maxWeight = 21;
int maxValue = 0;
void run(int i, int tw, int tv) {
if (i >= len) return;
tmp[i] = 0;
run(i + 1, tw, tv);
tmp[i] = 1;
if (tw + w[i] > maxWeight)
return;
if (tv + v[i] > maxValue) {
maxValue = tv + v[i];
for (int j = 0; j < len; j++) {
result[j] = tmp[j];
}
}
run(i + 1, tw + w[i], tv + v[i]);
}
void bag_main() {
for (int i = 0; i < len; i++) {
tmp[i] = 0;
result[i] = 0;
}
run(0, 0, 0);
for (int i = 0; i < len; i++) {
if(result[i])
printf("i:%d weight:%d value:%d\n",i, w[i], v[i]);
}
}
- 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