@z = [0,2,5] #教師が紙を持っている時に掛かる時間の配列です。 @w = [0,0,3] #生徒が紙を持っている時に掛かる時間の配列です。 def x(a) #教師が紙を持っている時に掛かる時間を計算します。 if @z[a] != nil return @z[a] end e = 3*a b = a/2 for i in 1..b c = i + 2 if x(i) >= y(a-i) c += x(i) else c += y(a-i) end d = i + 2 if y(i) >= x(a-i) d += y(i) else d += x(a-i) end if c <= d && e > c e = c elsif c > d && e > d e = d end end @z[a] = e return e end def y(a) #生徒が紙を持っている時に掛かる時間を計算します。 if @w[a] != nil return @w[a] end e = 3*a b = a/2 for i in 1..b c = i + 2 if y(i) >= y(a-i) c += y(i) else c += y(a-i) end if e > c e = c end end @w[a] = e return e end p x(42)