build
This commit is contained in:
parent
5f2154b49d
commit
79ade24902
@ -5,6 +5,11 @@ icon: material/help-circle-outline
|
||||
|
||||
# 16. 附录
|
||||
|
||||
<div class="center-table" markdown>
|
||||
|
||||
{ width="70%" }
|
||||
|
||||
</div>
|
||||
|
||||
## 本章内容
|
||||
|
||||
|
@ -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"
|
||||
|
@ -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"
|
||||
|
@ -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"
|
||||
|
@ -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"
|
||||
|
Loading…
Reference in New Issue
Block a user