1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
package main
import (
"testing"
)
func max(x, y int) int {
if x > y {
return x
}
return y
}
func cutRod(p []int, n int) int {
if n == 0 {
return 0
}
q := -1
for i := 1; i <= n; i++ {
q = max(q, p[i]+cutRod(p, n-i))
}
return q
}
func TestRodCutting(t *testing.T) {
r := []int{0, 1, 5, 8, 10, 13, 17, 18, 22, 25, 30}
p := []int{0, 1, 5, 8, 9, 10, 17, 17, 20, 24, 30}
t.Run("with recursive approach", func(t *testing.T) {
for n := 0; n < len(p); n++ {
expected := r[n]
actual := cutRod(p, n)
if expected != actual {
t.Errorf("%d: expected %d, actual %d", n, expected, actual)
}
}
})
}
|