编辑代码

#include <stdio.h>

#define MAX(a, b) ((a) > (b) ? (a) : (b))

void optimizeTravel(int num_spots, int total_days, int times[], int scores[]) {
    int dp[num_spots + 1][total_days + 1];
    int i, j;

    // 初始化dp数组
    for (i = 0; i <= num_spots; i++) {
        for (j = 0; j <= total_days; j++) {
            dp[i][j] = 0;
        }
    }

    // 动态规划计算最大评分
    for (i = 1; i <= num_spots; i++) {
        for (j = 1; j <= total_days; j++) {
            if (times[i] <= j) {
                dp[i][j] = MAX(dp[i-1][j-times[i]] + scores[i], dp[i-1][j]);
            } else {
                dp[i][j] = dp[i-1][j];
            }
        }
    }

    // 输出最大评分
    printf("去哪些景点能够获得最大价值:\n");
    i = num_spots;
    j = total_days;
    while (i > 0 && j > 0) {
        if (dp[i][j] != dp[i-1][j]) {
            printf("景点%d\n", i);
            j -= times[i];
        }
        i--;
    }
}

int main() {
    int num_spots = 4;
    int total_days = 4;
    int times[] = {0, 1, 2, 3, 1};  // 下标从1开始,0位置不使用
    int scores[] = {0, 7, 8, 9, 6};  // 下标从1开始,0位置不使用

    optimizeTravel(num_spots, total_days, times, scores);

    return 0;
}