编辑代码

public class TravelPlanner {

    static int[] days = {1, 2, 3, 1};
    static int[] scores = {7, 8, 9, 6};
    static String[] places = {"故宫", "颐和园", "长城", "天坛"};

    public static void findBestPlan(int totalDays) {
        int n = days.length;
        int[][] dp = new int[n + 1][totalDays + 1];

        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= totalDays; j++) {
                if (days[i - 1] <= j) {
                    dp[i][j] = Math.max(dp[i - 1][j], dp[i - 1][j - days[i - 1]] + scores[i - 1]);
                } else {
                    dp[i][j] = dp[i - 1][j];
                }
            }
        }

        int maxScore = dp[n][totalDays];
        System.out.println("Maximum Score: " + maxScore);

        int remainingDays = totalDays;
        for (int i = n; i > 0 && maxScore > 0; i--) {
            if (maxScore != dp[i-1][remainingDays]) {
                System.out.println("Visit: " + places[i-1]);
                maxScore -= scores[i-1];
                remainingDays -= days[i-1];
            }
        }
    }

    public static void main(String[] args) {
        findBestPlan(4); 
    }
}