hamster


私信TA

用户名:hamster992

访问量:1784

签 名:

哒哒哒哒哒打字人

等  级
排  名 2841
经  验 2047
参赛次数 1
文章发表 3
年  龄 21
在职情况 学生
学  校 桂林理工大学
专  业 网络工程

  自我简介:

解题思路:用01背包的动态规划解法来写

注意事项:注意输入的转换以及数组的下标问题,为了方便表示把结果的dp数组的第一位置零

参考代码:

import java.util.Scanner;


public class Main {


    public static void main(String[] args){

    Scanner scan = new Scanner(System.in);

    String[] input = scan.nextLine().split(" ");

    int Time = Integer.parseInt(input[0]);

    int Type = Integer.parseInt(input[1]);

    int[] time = new int[Type];

    int[] value = new int[Type];

    for (int i = 0; i < Type; i++) {

    input = scan.nextLine().split(" ");

    time[i] = Integer.parseInt(input[0]);

    value[i] = Integer.parseInt(input[1]);

    }

    scan.close();

    System.out.println(findmed(Time,Type,time,value));

   

    }//main

    public static int findmed (int T, int type, int[] time, int[] value) {

    int[] dp = new int[T+1];

    for (int i = 1; i < type + 1; i++) {

    for (int j = T; j >= time[i-1]; j--) {

    dp[j] = Math.max(dp[j], dp[j-time[i-1]] + value[i-1]);

    }

    }

    return dp[T];

    }

}//Class Main


 

0.0分

1 人评分

看不懂代码?想转换其他语言的代码? 或者想问其他问题? 试试问问AI编程助手,随时响应你的问题:

编程语言转换万能编程问答  

代码解释器

代码纠错

SQL生成与解释

  评论区