import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int row = sc.nextInt();
int col = sc.nextInt();
int[] values = new int[row + 1];
int[] weights = new int[row + 1];
for (int i = 1; i <= row; i++) {
weights[i] = sc.nextInt();
values[i] = sc.nextInt();
}
int[][] dp = new int[row + 1][col + 1];
for (int i = weights[0]; i <= col; i++) {
dp[0][i] = weights[0];
}
for (int i = 1; i <= row; i++) {
for (int j = 1; j <= col; j++) {
if (weights[i] > j) {
dp[i][j] = dp[i - 1][j];
} else {
dp[i][j] = Math.max(dp[i - 1][j], dp[i - 1][j - weights[i]] + values[i]);
}
}
}
System.out.println("最终结果: " + dp[row][col]);
// 输出Markdown表格
StringBuilder table = new StringBuilder("| |");
for (int j = 0; j <= col; j++) {
table.append(" ").append(j).append(" |");
}
table.append("\n|:-:|");
for (int j = 0; j <= col; j++) {
table.append(":-:|");
}
table.append("\n");
for (int i = 0; i <= row; i++) {
StringBuilder rowString = new StringBuilder("| ").append(i).append(" |");
for (int j = 0; j <= col; j++) {
rowString.append(" ").append(dp[i][j]).append(" |");
}
table.append(rowString).append("\n");
}
System.out.println(table.toString());
}
}
|
0 |
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
0 |
0 |
0 |
0 |
0 |
0 |
0 |
0 |
0 |
0 |
0 |
0 |
1 |
0 |
0 |
1 |
1 |
1 |
1 |
1 |
1 |
1 |
1 |
1 |
2 |
0 |
0 |
1 |
3 |
3 |
4 |
4 |
4 |
4 |
4 |
4 |
3 |
0 |
0 |
1 |
3 |
6 |
6 |
7 |
9 |
9 |
10 |
10 |
4 |
0 |
0 |
1 |
3 |
6 |
6 |
7 |
9 |
10 |
10 |
11 |
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int row = sc.nextInt();
int col = sc.nextInt();
int[] values = new int[row + 1];
int[] weights = new int[row + 1];
for (int i = 1; i <= row; i++) {
weights[i] = sc.nextInt();
values[i] = sc.nextInt();
}
int[] dp = new int[col + 1];
// 输出Markdown表格的StringBuilder
StringBuilder table = new StringBuilder("| |");
for (int j = 0; j <= col; j++) {
table.append(" ").append(j).append(" |");
}
table.append("\n|:-:|");
for (int j = 0; j <= col; j++) {
table.append(":-:|");
}
table.append("\n");
for (int i = weights[0]; i <= col; i++) {
dp[i] = weights[0];
}
for (int i = 1; i <= row; i++) {
for (int j = col; j >= weights[i]; j--) {
dp[j] = Math.max(dp[j], dp[j - weights[i]] + values[i]);
}
// 将当前状态添加到表格中
table.append("| ").append(i).append(" |");
for (int value : dp) {
table.append(" ").append(value).append(" |");
}
table.append("\n");
}
System.out.println("最终结果: " + dp[col]);
// 打印Markdown表格
System.out.println(table.toString());
}
}
|
0 |
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
1 |
0 |
0 |
1 |
1 |
1 |
1 |
1 |
1 |
1 |
1 |
1 |
2 |
0 |
0 |
1 |
3 |
3 |
4 |
4 |
4 |
4 |
4 |
4 |
3 |
0 |
0 |
1 |
3 |
6 |
6 |
7 |
9 |
9 |
10 |
10 |
4 |
0 |
0 |
1 |
3 |
6 |
6 |
7 |
9 |
10 |
10 |
11 |
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int row = sc.nextInt();
int col = sc.nextInt();
int[] weights = new int[row + 1];
int[] values = new int[row + 1];
int[][] dp = new int[row + 1][col + 1];
for (int i = 1; i <= row; i++) {
weights[i] = sc.nextInt();
values[i] = sc.nextInt();
}
for (int i = 1; i <= row; i++) {
for (int j = 1; j <= col; j++) {
if (j >= weights[i]) {
dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - weights[i]] + values[i]);
} else {
dp[i][j] = dp[i - 1][j];
}
}
}
// 输出Markdown表格
System.out.print("| |");
for (int j = 0; j <= col; j++) {
System.out.print(" " + j + " |");
}
System.out.print("\n|:-:|");
for (int j = 0; j <= col; j++) {
System.out.print(":-:|");
}
System.out.println();
for (int i = 0; i <= row; i++) {
System.out.print("| " + i + " |");
for (int j = 0; j <= col; j++) {
System.out.print(" " + dp[i][j] + " |");
}
System.out.println();
}
}
}
|
0 |
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
0 |
0 |
0 |
0 |
0 |
0 |
0 |
0 |
0 |
0 |
0 |
0 |
1 |
0 |
0 |
1 |
1 |
2 |
2 |
3 |
3 |
4 |
4 |
5 |
2 |
0 |
0 |
1 |
3 |
3 |
4 |
6 |
6 |
7 |
9 |
9 |
3 |
0 |
0 |
1 |
3 |
6 |
6 |
7 |
9 |
12 |
12 |
13 |
4 |
0 |
0 |
1 |
3 |
6 |
6 |
7 |
9 |
12 |
12 |
13 |
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int row = sc.nextInt();
int col = sc.nextInt();
int[] weights = new int[row + 1];
int[] values = new int[row + 1];
int[] dp = new int[col + 1]; // 一维数组替代二维数组
for (int i = 1; i <= row; i++) {
weights[i] = sc.nextInt();
values[i] = sc.nextInt();
}
// 输出Markdown表格的StringBuilder
StringBuilder table = new StringBuilder("| |");
for (int j = 0; j <= col; j++) {
table.append(" ").append(j).append(" |");
}
table.append("\n|:-:|");
for (int j = 0; j <= col; j++) {
table.append(":-:|");
}
table.append("\n");
for (int i = 1; i <= row; i++) {
for (int j = weights[i]; j <= col; j++) {
dp[j] = Math.max(dp[j], dp[j - weights[i]] + values[i]);
}
// 将当前状态添加到表格中
table.append("| ").append(i).append(" |");
for (int value : dp) {
table.append(" ").append(value).append(" |");
}
table.append("\n");
}
// 打印Markdown表格
System.out.println(table.toString());
System.out.println("最终结果: " + dp[col]);
}
}
|
0 |
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
1 |
0 |
0 |
1 |
1 |
2 |
2 |
3 |
3 |
4 |
4 |
5 |
2 |
0 |
0 |
1 |
3 |
3 |
4 |
6 |
6 |
7 |
9 |
9 |
3 |
0 |
0 |
1 |
3 |
6 |
6 |
7 |
9 |
12 |
12 |
13 |
4 |
0 |
0 |
1 |
3 |
6 |
6 |
7 |
9 |
12 |
12 |
13 |