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 c70316d

Browse files
committed
添加 背包问题理论基础完全背包.md Scala版本
1 parent 634c051 commit c70316d

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

‎problems/背包问题理论基础完全背包.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,27 @@ function test_CompletePack(): void {
359359
test_CompletePack();
360360
```
361361

362+
Scala:
362363

364+
```scala
365+
// 先遍历物品,再遍历背包容量
366+
object Solution {
367+
def test_CompletePack() {
368+
var weight = Array[Int](1, 3, 4)
369+
var value = Array[Int](15, 20, 30)
370+
var baseweight = 4
371+
372+
var dp = new Array[Int](baseweight + 1)
373+
374+
for (i <- 0 until weight.length) {
375+
for (j <- weight(i) to baseweight) {
376+
dp(j) = math.max(dp(j), dp(j - weight(i)) + value(i))
377+
}
378+
}
379+
dp(baseweight)
380+
}
381+
}
382+
```
363383

364384

365385
-----------------------

0 commit comments

Comments
(0)

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