From a2d340b00279aac08df7cbba7a5733416dc28920 Mon Sep 17 00:00:00 2001 From: ZongqinWang <1722249371@qq.com> Date: Sun, 19 Jun 2022 21:32:10 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20=E8=83=8C=E5=8C=85?= =?UTF-8?q?=E7=90=86=E8=AE=BA=E5=9F=BA=E7=A1=8001=E8=83=8C=E5=8C=85-2.md?= =?UTF-8?q?=20Scala=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/背包理论基础01背包-2.md | 26 ++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/problems/背包理论基础01背包-2.md b/problems/背包理论基础01背包-2.md index b66b74a6..81e61be4 100644 --- a/problems/背包理论基础01背包-2.md +++ b/problems/背包理论基础01背包-2.md @@ -375,7 +375,33 @@ console.log(testWeightBagProblem(weight, value, size)); ``` +### Scala +```scala +object Solution { + // 滚动数组 + def test_1_wei_bag_problem(): Unit = { + var weight = Array[Int](1, 3, 4) + var value = Array[Int](15, 20, 30) + var baseweight = 4 + + // dp数组 + var dp = new Array[Int](baseweight + 1) + + // 遍历 + for (i <- 0 until weight.length; j <- baseweight to weight(i) by -1) { + dp(j) = math.max(dp(j), dp(j - weight(i)) + value(i)) + } + + // 打印数组 + println("[" + dp.mkString(",") + "]") + } + + def main(args: Array[String]): Unit = { + test_1_wei_bag_problem() + } +} +``` -----------------------