-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdynamic_programming.rb
80 lines (63 loc) · 1.33 KB
/
dynamic_programming.rb
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# coding:utf-8
# this code was cited from algorithm introduction no.2 p8-12
def print_cut_rod_solution(p,n)
end
#価格の配列
$p=[0,1,5,8,9,10,17,17,20,24,30,1,5,8,9,10,17,17,20,24,30,1,5,8,9,10,17,17,20,24,30,1,5,8,9,10,17,17,20,24,30]
#棒の長さ
$n=4
#再帰的トップ型ロッド切り出し->遅い
def cut_rod(p,n)
if n==0
return 0 #棒の長さが0だから収入も0円
end
q=-100000
for i in 1..n
q=[q,p[i]+cut_rod($p,n-i)].max
end
#切り出した分のお金
#切り出した残りの棒も全部区切ってどれが一番高いかを調べる
return q
end
def bottom_up_cut_rod(p,n)
r = Array.new(n)
r[0]=0
for j in 1..n
q=-100000
for i in 1..j
q=[q,p[i]+r[j-i]].max
end
r[j]=q
end
return r[n]
end
def extended_bottom_up_cut_rod(p,n)
r=Array.new(n)#お金
s=Array.new(n)#切り取る棒の長さ
r[0]=0
for j in 1..n
q=-10000
for i in 1..j
if q<p[i]+r[j-i]
q=p[i]+r[j-i]
s[j]=i
end
end
r[j]=q
end
return r,s
end
def print_cut_rod_solution(p,n)
r,s=extended_bottom_up_cut_rod(p,n)
while n>0
puts "cut rod -> #{s[n]}"
puts "get money -> #{r[n]}"
n=n-s[n]
end
end
#main
#遅いやつ
#p cut_rod($p,$n)
#p bottom_up_cut_rod($p,$n)
#puts extended_bottom_up_cut_rod($p,$n)
puts print_cut_rod_solution($p,7)