From 79ade249027f6872ae3546b0d2afca4ea2484145 Mon Sep 17 00:00:00 2001 From: krahets Date: Sat, 22 Jul 2023 03:58:19 +0800 Subject: [PATCH] build --- chapter_appendix/index.md | 5 +++ chapter_greedy/fractional_knapsack_problem.md | 37 ++++++++++++++++++- chapter_greedy/greedy_algorithm.md | 19 +++++++++- chapter_greedy/max_capacity_problem.md | 21 ++++++++++- chapter_greedy/max_product_cutting_problem.md | 21 ++++++++++- 5 files changed, 98 insertions(+), 5 deletions(-) diff --git a/chapter_appendix/index.md b/chapter_appendix/index.md index 057aa055e..32a30e4ca 100644 --- a/chapter_appendix/index.md +++ b/chapter_appendix/index.md @@ -5,6 +5,11 @@ icon: material/help-circle-outline # 16.   附录 +
+ +![附录](../assets/covers/chapter_appendix.jpg){ width="70%" } + +
## 本章内容 diff --git a/chapter_greedy/fractional_knapsack_problem.md b/chapter_greedy/fractional_knapsack_problem.md index 30da64082..d9a297477 100644 --- a/chapter_greedy/fractional_knapsack_problem.md +++ b/chapter_greedy/fractional_knapsack_problem.md @@ -189,9 +189,42 @@ status: new === "C#" ```csharp title="fractional_knapsack.cs" - [class]{Item}-[func]{} + /* 物品 */ + class Item { + public int w; // 物品重量 + public int v; // 物品价值 - [class]{fractional_knapsack}-[func]{fractionalKnapsack} + public Item(int w, int v) { + this.w = w; + this.v = v; + } + } + + /* 分数背包:贪心 */ + double fractionalKnapsack(int[] wgt, int[] val, int cap) { + // 创建物品列表,包含两个属性:重量、价值 + Item[] items = new Item[wgt.Length]; + for (int i = 0; i < wgt.Length; i++) { + items[i] = new Item(wgt[i], val[i]); + } + // 按照单位价值 item.v / item.w 从高到低进行排序 + Array.Sort(items, (x, y) => (y.v / y.w).CompareTo(x.v / x.w)); + // 循环贪心选择 + double res = 0; + foreach (Item item in items) { + if (item.w <= cap) { + // 若剩余容量充足,则将当前物品整个装进背包 + res += item.v; + cap -= item.w; + } else { + // 若剩余容量不足,则将当前物品的一部分装进背包 + res += (double)item.v / item.w * cap; + // 已无剩余容量,因此跳出循环 + break; + } + } + return res; + } ``` === "Swift" diff --git a/chapter_greedy/greedy_algorithm.md b/chapter_greedy/greedy_algorithm.md index 8d01193e2..fe133d299 100644 --- a/chapter_greedy/greedy_algorithm.md +++ b/chapter_greedy/greedy_algorithm.md @@ -119,7 +119,24 @@ status: new === "C#" ```csharp title="coin_change_greedy.cs" - [class]{coin_change_greedy}-[func]{coinChangeGreedy} + /* 零钱兑换:贪心 */ + int coinChangeGreedy(int[] coins, int amt) { + // 假设 coins 列表有序 + int i = coins.Length - 1; + int count = 0; + // 循环进行贪心选择,直到无剩余金额 + while (amt > 0) { + // 找到小于且最接近剩余金额的硬币 + while (coins[i] > amt) { + i--; + } + // 选择 coins[i] + amt -= coins[i]; + count++; + } + // 若未找到可行方案,则返回 -1 + return amt == 0 ? count : -1; + } ``` === "Swift" diff --git a/chapter_greedy/max_capacity_problem.md b/chapter_greedy/max_capacity_problem.md index c67d731d4..de6909034 100644 --- a/chapter_greedy/max_capacity_problem.md +++ b/chapter_greedy/max_capacity_problem.md @@ -185,7 +185,26 @@ $$ === "C#" ```csharp title="max_capacity.cs" - [class]{max_capacity}-[func]{maxCapacity} + /* 最大容量:贪心 */ + int maxCapacity(int[] ht) { + // 初始化 i, j 分列数组两端 + int i = 0, j = ht.Length - 1; + // 初始最大容量为 0 + int res = 0; + // 循环贪心选择,直至两板相遇 + while (i < j) { + // 更新最大容量 + int cap = Math.Min(ht[i], ht[j]) * (j - i); + res = Math.Max(res, cap); + // 向内移动短板 + if (ht[i] < ht[j]) { + i++; + } else { + j--; + } + } + return res; + } ``` === "Swift" diff --git a/chapter_greedy/max_product_cutting_problem.md b/chapter_greedy/max_product_cutting_problem.md index 065c476e1..79c7cbb84 100644 --- a/chapter_greedy/max_product_cutting_problem.md +++ b/chapter_greedy/max_product_cutting_problem.md @@ -171,7 +171,26 @@ $$ === "C#" ```csharp title="max_product_cutting.cs" - [class]{max_product_cutting}-[func]{maxProductCutting} + /* 最大切分乘积:贪心 */ + int maxProductCutting(int n) { + // 当 n <= 3 时,必须切分出一个 1 + if (n <= 3) { + return 1 * (n - 1); + } + // 贪心地切分出 3 ,a 为 3 的个数,b 为余数 + int a = n / 3; + int b = n % 3; + if (b == 1) { + // 当余数为 1 时,将一对 1 * 3 转化为 2 * 2 + return (int)Math.Pow(3, a - 1) * 2 * 2; + } + if (b == 2) { + // 当余数为 2 时,不做处理 + return (int)Math.Pow(3, a) * 2; + } + // 当余数为 0 时,不做处理 + return (int)Math.Pow(3, a); + } ``` === "Swift"