mirror of
https://github.com/krahets/hello-algo.git
synced 2026-05-12 03:27:44 +08:00
build
This commit is contained in:
@@ -456,6 +456,78 @@ comments: true
|
||||
}
|
||||
```
|
||||
|
||||
=== "Kotlin"
|
||||
|
||||
```kotlin title="fractional_knapsack.kt"
|
||||
/* 物品 */
|
||||
class Item(
|
||||
val w: Int, // 物品
|
||||
val v: Int // 物品价值
|
||||
)
|
||||
|
||||
/* 分数背包:贪心 */
|
||||
fun fractionalKnapsack(
|
||||
wgt: IntArray,
|
||||
value: IntArray,
|
||||
c: Int
|
||||
): Double {
|
||||
// 创建物品列表,包含两个属性:重量、价值
|
||||
var cap = c
|
||||
val items = arrayOfNulls<Item>(wgt.size)
|
||||
for (i in wgt.indices) {
|
||||
items[i] = Item(wgt[i], value[i])
|
||||
}
|
||||
// 按照单位价值 item.v / item.w 从高到低进行排序
|
||||
Arrays.sort(items, Comparator.comparingDouble { item: Item -> -(item.v.toDouble() / item.w) })
|
||||
// 循环贪心选择
|
||||
var res = 0.0
|
||||
for (item in items) {
|
||||
if (item!!.w <= cap) {
|
||||
// 若剩余容量充足,则将当前物品整个装进背包
|
||||
res += item.v.toDouble()
|
||||
cap -= item.w
|
||||
} else {
|
||||
// 若剩余容量不足,则将当前物品的一部分装进背包
|
||||
res += item.v.toDouble() / item.w * cap
|
||||
// 已无剩余容量,因此跳出循环
|
||||
break
|
||||
}
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
/* 分数背包:贪心 */
|
||||
fun fractionalKnapsack(
|
||||
wgt: IntArray,
|
||||
value: IntArray,
|
||||
c: Int
|
||||
): Double {
|
||||
// 创建物品列表,包含两个属性:重量、价值
|
||||
var cap = c
|
||||
val items = arrayOfNulls<Item>(wgt.size)
|
||||
for (i in wgt.indices) {
|
||||
items[i] = Item(wgt[i], value[i])
|
||||
}
|
||||
// 按照单位价值 item.v / item.w 从高到低进行排序
|
||||
Arrays.sort(items, Comparator.comparingDouble { item: Item -> -(item.v.toDouble() / item.w) })
|
||||
// 循环贪心选择
|
||||
var res = 0.0
|
||||
for (item in items) {
|
||||
if (item!!.w <= cap) {
|
||||
// 若剩余容量充足,则将当前物品整个装进背包
|
||||
res += item.v.toDouble()
|
||||
cap -= item.w
|
||||
} else {
|
||||
// 若剩余容量不足,则将当前物品的一部分装进背包
|
||||
res += item.v.toDouble() / item.w * cap
|
||||
// 已无剩余容量,因此跳出循环
|
||||
break
|
||||
}
|
||||
}
|
||||
return res
|
||||
}
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
|
||||
```zig title="fractional_knapsack.zig"
|
||||
|
||||
Reference in New Issue
Block a user