Retranslate the complete English edition from Simplified Chinese (#2010)

* Retranslate the complete English edition from Simplified Chinese

* Polish three English explanations after reader-focused review
This commit is contained in:
Yudong Jin
2026-10-31 03:35:51 +08:00
committed by GitHub
parent ef46e2ed95
commit 14c314c08c
1502 changed files with 29459 additions and 23517 deletions

View File

@@ -21,26 +21,26 @@ class Item {
}
public class fractional_knapsack {
/* Fractional knapsack: Greedy algorithm */
/* Fractional knapsack: greedy algorithm */
static double fractionalKnapsack(int[] wgt, int[] val, int cap) {
// Create item list with two attributes: weight, value
// Create a list of items, each with two attributes: weight and value
Item[] items = new Item[wgt.length];
for (int i = 0; i < wgt.length; i++) {
items[i] = new Item(wgt[i], val[i]);
}
// Sort by unit value item.v / item.w from high to low
// Sort by value per unit weight, item.v / item.w, in descending order
Arrays.sort(items, Comparator.comparingDouble(item -> -((double) item.v / item.w)));
// Loop for greedy selection
// Repeatedly make greedy choices
double res = 0;
for (Item item : items) {
if (item.w <= cap) {
// If remaining capacity is sufficient, put the entire current item into the knapsack
// If enough capacity remains, put the entire current item in the knapsack
res += item.v;
cap -= item.w;
} else {
// If remaining capacity is insufficient, put part of the current item into the knapsack
// If the remaining capacity is insufficient, put part of the current item in the knapsack
res += (double) item.v / item.w * cap;
// No remaining capacity, so break out of the loop
// No capacity remains, so exit the loop
break;
}
}
@@ -54,6 +54,6 @@ public class fractional_knapsack {
// Greedy algorithm
double res = fractionalKnapsack(wgt, val, cap);
System.out.println("Maximum item value not exceeding knapsack capacity is " + res);
System.out.println("Maximum total item value within the knapsack capacity: " + res);
}
}