mirror of
https://github.com/krahets/hello-algo.git
synced 2024-12-26 11:26:29 +08:00
7359a7cb4b
* feat(swift): review for chapter_computational_complexity * feat(swift): review for chapter_data_structure * feat(swift): review for chapter_array_and_linkedlist * feat(swift): review for chapter_stack_and_queue * feat(swift): review for chapter_hashing * feat(swift): review for chapter_tree * feat(swift): add codes for heap article * feat(swift): review for chapter_heap * feat(swift): review for chapter_graph * feat(swift): review for chapter_searching * feat(swift): review for chapter_sorting * feat(swift): review for chapter_divide_and_conquer * feat(swift): review for chapter_backtracking * feat(swift): review for chapter_dynamic_programming * feat(swift): review for chapter_greedy * feat(swift): review for utils * feat(swift): update ci tool * feat(swift): trailing closure * feat(swift): array init * feat(swift): map index
57 lines
1.5 KiB
Swift
57 lines
1.5 KiB
Swift
/**
|
||
* File: fractional_knapsack.swift
|
||
* Created Time: 2023-09-03
|
||
* Author: nuomi1 (nuomi1@qq.com)
|
||
*/
|
||
|
||
/* 物品 */
|
||
class Item {
|
||
var w: Int // 物品重量
|
||
var v: Int // 物品价值
|
||
|
||
init(w: Int, v: Int) {
|
||
self.w = w
|
||
self.v = v
|
||
}
|
||
}
|
||
|
||
/* 分数背包:贪心 */
|
||
func fractionalKnapsack(wgt: [Int], val: [Int], cap: Int) -> Double {
|
||
// 创建物品列表,包含两个属性:重量、价值
|
||
var items = zip(wgt, val).map { Item(w: $0, v: $1) }
|
||
// 按照单位价值 item.v / item.w 从高到低进行排序
|
||
items.sort { -(Double($0.v) / Double($0.w)) < -(Double($1.v) / Double($1.w)) }
|
||
// 循环贪心选择
|
||
var res = 0.0
|
||
var cap = cap
|
||
for item in items {
|
||
if item.w <= cap {
|
||
// 若剩余容量充足,则将当前物品整个装进背包
|
||
res += Double(item.v)
|
||
cap -= item.w
|
||
} else {
|
||
// 若剩余容量不足,则将当前物品的一部分装进背包
|
||
res += Double(item.v) / Double(item.w) * Double(cap)
|
||
// 已无剩余容量,因此跳出循环
|
||
break
|
||
}
|
||
}
|
||
return res
|
||
}
|
||
|
||
@main
|
||
enum FractionalKnapsack {
|
||
/* Driver Code */
|
||
static func main() {
|
||
// 物品重量
|
||
let wgt = [10, 20, 30, 40, 50]
|
||
// 物品价值
|
||
let val = [50, 120, 150, 210, 240]
|
||
// 背包容量
|
||
let cap = 50
|
||
|
||
// 贪心算法
|
||
let res = fractionalKnapsack(wgt: wgt, val: val, cap: cap)
|
||
print("不超过背包容量的最大物品价值为 \(res)")
|
||
}
|
||
}
|