01背包
//01背包 void WKnapsack3(int *value, int* v, int n, int V) { int d[100]; memset(d, 0, sizeof(int)* 100); bool visit[100][100]; memset(visit, 0, sizeof(bool)* 100 * 100); for (int i = 0; i < n; i++) { for (int j = V; j >= v[i]; j--) { if (j >= v[i]) { if (d[j] < d[j - v[i]] + value[i]) visit[i][j] = true; d[j] = max(d[j], d[j - v[i]] + value[i]); } } } printf("%d ", d[V]); printf("路径:\n"); for (int i = n-1, j = V; i>=0 && j>0 ; i--)//打印 路径 { if (visit[i][j]) { printf("%d ", value[i]); j = j - v[i]; } } }