编辑代码

#include <stdio.h>
#include <string.h>

#define MAX_PlaceS 4
#define TOTAL_DAYS 4

struct Place {
    char name[20];
    int days;
    int score;


};

void placeFunc(struct Place Places[], int days) {
    int dp[MAX_PlaceS + 1][days + 1];
    for (int i = 0; i <= MAX_PlaceS; ++i) {
        for (int j = 0; j <= days; ++j) {
            if (i == 0 || j == 0)
                dp[i][j] = 0;
            else if (Places[i - 1].days <= j)
                dp[i][j] = 
                    (Places[i - 1].score + dp[i - 1][j - Places[i - 1].days] > dp[i - 1][j]) ?
                    (Places[i - 1].score + dp[i - 1][j - Places[i - 1].days]) : dp[i - 1][j];
            else
                dp[i][j] = dp[i - 1][j];
        }
    }
    int maxScore = dp[MAX_PlaceS][days];
    printf("最优的价值为:%d\n", maxScore);
    printf("景点有:\n");
    int i = MAX_PlaceS, j = days;
    while (i > 0 && j > 0) {
        if (dp[i][j] != dp[i - 1][j]) {
            printf("%s\n", Places[i - 1].name);
            j -= Places[i - 1].days;
        }
        --i;
    }
}

int main() {
    struct Place Places[MAX_PlaceS] = {
        {"故宫", 1, 7},
        {"颐和园", 2, 8},
        {"长城", 3, 9},
        {"天坛", 1, 6}
    };
    int totalDays = TOTAL_DAYS;
    placeFunc(Places, totalDays);
    return 0;
}