Many bug fixes and improvements (#1270)

* Add Ruby and Kotlin icons
Add the avatar of @curtishd

* Update README

* Synchronize zh-hant and zh versions.

* Translate the pythontutor blocks to traditional Chinese

* Fix en/mkdocs.yml

* Update the landing page of the en version.

* Fix the Dockerfile

* Refine the en landingpage

* Fix en landing page

* Reset the README.md
This commit is contained in:
Yudong Jin
2024-04-11 20:18:19 +08:00
committed by GitHub
parent 07977184ad
commit b2f0d4603d
192 changed files with 2382 additions and 1196 deletions
@@ -6,34 +6,28 @@
package chapter_greedy
import java.util.*
/* 物品 */
class Item(
val w: Int, // 物品
val v: Int // 物品價值
val v: Int // 物品價值
)
/* 分數背包:貪婪 */
fun fractionalKnapsack(
wgt: IntArray,
value: IntArray,
c: Int
): Double {
fun fractionalKnapsack(wgt: IntArray, _val: 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])
items[i] = Item(wgt[i], _val[i])
}
// 按照單位價值 item.v / item.w 從高到低進行排序
Arrays.sort(items, Comparator.comparingDouble { item: Item -> -(item.v.toDouble() / item.w) })
items.sortBy { item: Item? -> -(item!!.v.toDouble() / item.w) }
// 迴圈貪婪選擇
var res = 0.0
for (item in items) {
if (item!!.w <= cap) {
// 若剩餘容量充足,則將當前物品整個裝進背包
res += item.v.toDouble()
res += item.v
cap -= item.w
} else {
// 若剩餘容量不足,則將當前物品的一部分裝進背包
@@ -48,10 +42,10 @@ fun fractionalKnapsack(
/* Driver Code */
fun main() {
val wgt = intArrayOf(10, 20, 30, 40, 50)
val values = intArrayOf(50, 120, 150, 210, 240)
val _val = intArrayOf(50, 120, 150, 210, 240)
val cap = 50
// 貪婪演算法
val res = fractionalKnapsack(wgt, values, cap)
val res = fractionalKnapsack(wgt, _val, cap)
println("不超過背包容量的最大物品價值為 $res")
}
@@ -19,8 +19,8 @@ fun maxCapacity(ht: IntArray): Int {
// 迴圈貪婪選擇,直至兩板相遇
while (i < j) {
// 更新最大容量
val cap = (min(ht[i].toDouble(), ht[j].toDouble()) * (j - i)).toInt()
res = max(res.toDouble(), cap.toDouble()).toInt()
val cap = min(ht[i], ht[j]) * (j - i)
res = max(res, cap)
// 向內移動短板
if (ht[i] < ht[j]) {
i++
@@ -19,14 +19,14 @@ fun maxProductCutting(n: Int): Int {
val b = n % 3
if (b == 1) {
// 當餘數為 1 時,將一對 1 * 3 轉化為 2 * 2
return 3.0.pow((a - 1).toDouble()).toInt() * 2 * 2
return 3.0.pow((a - 1)).toInt() * 2 * 2
}
if (b == 2) {
// 當餘數為 2 時,不做處理
return 3.0.pow(a.toDouble()).toInt() * 2 * 2
return 3.0.pow(a).toInt() * 2 * 2
}
// 當餘數為 0 時,不做處理
return 3.0.pow(a.toDouble()).toInt()
return 3.0.pow(a).toInt()
}
/* Driver Code */
@@ -36,4 +36,4 @@ fun main() {
// 貪婪演算法
val res = maxProductCutting(n)
println("最大切分乘積為 $res")
}
}