import java.util.Scanner; public class zroe背包 { public static void main(String[] args){ Scanner sc=new Scanner(System.in); int n=sc.nextInt();//物品 int m=sc.nextInt();//容量 int[] n1=new int[n+1]; int[] m1=new int[n+1]; for (int i = 1; i <= n; i++) { n1[i]=sc.nextInt(); m1[i]=sc.nextInt();//容量 } int[][] w=new int[n+1][m+1]; for (int i = 1; i <= n ; i++) { for (int j = 1; j <= m; j++) { if (j<n1[i]) { w[i][j]=w[i-1][j]; } else { w[i][j]=Math.max(w[i-1][j],w[i-1][j-n1[i]]+m1[i]); } } } System.out.println(w[n][m]); } }
解题思路:
注意事项:
参考代码:
0.0分
1 人评分
点我有惊喜!你懂得!浏览:1393 |
简单的a+b (C语言代码)浏览:717 |
2006年春浙江省计算机等级考试二级C 编程题(2) (C语言代码)浏览:468 |
奖学金 (C++代码)浏览:2005 |
C语言训练-排序问题<1> (C++代码)浏览:589 |
数组输出 (C语言代码)--此题的题目描述有问题浏览:1814 |
C语言程序设计教程(第三版)课后习题5.4 (C语言代码)浏览:1282 |
printf基础练习2 (有点不明白)浏览:836 |
WU-复数求和 (C++代码)浏览:1995 |
简单的a+b (C语言代码)浏览:596 |