编辑代码

public class TravelOptimization {
    public static void travelOptimization(int[] time, int[] score, int totalDays) {
        int n = time.length;
        int[][] dp = new int[n + 1][totalDays + 1];

        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= totalDays; j++) {
                if (j >= time[i - 1]) {
                    dp[i][j] = Math.max(dp[i - 1][j], dp[i - 1][j - time[i - 1]] + score[i - 1]);
                } else {
                    dp[i][j] = dp[i - 1][j];
                }
            }
        }
        int maxScore = dp[n][totalDays];
        System.out.println("最大价值为:" + maxScore);
        System.out.println("选择的景点有:");
        int daysLeft = totalDays;
        for (int i = n; i > 0 && maxScore > 0; i--) {
            if (maxScore != dp[i - 1][daysLeft]) {
                System.out.println("景点" + i + "(时间:" + time[i - 1] + "天,评分:" + score[i - 1] + ")");
                maxScore -= score[i - 1];
                daysLeft -= time[i - 1];
            }
        }
    }
    public static void main(String[] args) {
        //景点对应所花的时间:故宫:1天,颐和园:2天;长城:3天;天坛:1天;
        int[] time = {1, 2, 3, 1};
        //景点对应的评分:故宫:7分;颐和园:8分;长城:9分;天坛:6分
        int[] score = {7, 8, 9, 6};
        //假期总天数为4天
        int totalDays = 4;

        travelOptimization(time, score, totalDays);
    }
}