解题思路:
注意事项:
参考代码:
Scanner scanner=new Scanner(System.in); int m=scanner.nextInt(); int n=scanner.nextInt(); int [][] arr=new int [n+1][m+1]; int [] v=new int [n+1]; int [] c=new int [n+1]; for (int i = 1; i <= n; i++) { v[i]=scanner.nextInt(); c[i]=scanner.nextInt(); } for (int i = 1; i <= n; i++) { for (int j = 1; j <=m; j++) { for (int k = 0; k*v[i]<=m ; k++) { if (j<k*v[i]) { arr[i][j]=arr[i][j]; }else { arr[i][j]=Math.max(arr[i][j], arr[i-1][j-k*v[i]]+k*c[i]); } } } } System.out.println("max="+arr[n][m]);
0.0分
0 人评分