Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit a2d340b

Browse files
committed
添加 背包理论基础01背包-2.md Scala版本
1 parent 106430d commit a2d340b

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

‎problems/背包理论基础01背包-2.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,33 @@ console.log(testWeightBagProblem(weight, value, size));
375375
376376
```
377377

378+
### Scala
379+
380+
```scala
381+
object Solution {
382+
// 滚动数组
383+
def test_1_wei_bag_problem(): Unit = {
384+
var weight = Array[Int](1, 3, 4)
385+
var value = Array[Int](15, 20, 30)
386+
var baseweight = 4
387+
388+
// dp数组
389+
var dp = new Array[Int](baseweight + 1)
390+
391+
// 遍历
392+
for (i <- 0 until weight.length; j <- baseweight to weight(i) by -1) {
393+
dp(j) = math.max(dp(j), dp(j - weight(i)) + value(i))
394+
}
395+
396+
// 打印数组
397+
println("[" + dp.mkString(",") + "]")
398+
}
378399

400+
def main(args: Array[String]): Unit = {
401+
test_1_wei_bag_problem()
402+
}
403+
}
404+
```
379405

380406
-----------------------
381407
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /