#include <stdio.h>
#define MAX_DAYS 4
#define NUM_SPOTS 4
struct Spot {
char name[20];
int days;
int score;
};
void knapsack(struct Spot spots[], int n) {
int dp[MAX_DAYS + 1] = {0};
int selected[NUM_SPOTS][MAX_DAYS + 1] = {0};
for (int i = 0; i < n; ++i) {
for (int j = MAX_DAYS; j >= spots[i].days; --j) {
if (dp[j] < dp[j - spots[i].days] + spots[i].score) {
dp[j] = dp[j - spots[i].days] + spots[i].score;
selected[i][j] = 1;
}
}
}
printf("选取的景点为:");
int daysLeft = MAX_DAYS;
for (int i = n - 1; i >= 0; --i) {
if (selected[i][daysLeft]) {
printf("%s ", spots[i].name);
daysLeft -= spots[i].days;
}
}
printf("\n获得最大价值评分为:%d", dp[MAX_DAYS]);
}
int main() {
struct Spot spots[NUM_SPOTS] = {
{"故宫", 1, 7},
{"颐和园", 2, 8},
{"长城", 3, 9},
{"天坛", 1, 6}
};
knapsack(spots, NUM_SPOTS);
return 0;
}