- 論壇徽章:
- 0
|
為解決如下問題:
有一根300厘米的細(xì)木桿,在第30厘米、80厘米、110厘米、160厘米、250厘米這五個位置上各有一只螞蟻。木桿很細(xì),不能同時通過
兩只螞蟻。開始時,螞蟻的頭朝左還是朝右是任意的,它們只會朝前走或調(diào)頭,但不會后退。當(dāng)任意兩只螞蟻碰頭時,兩只螞蟻會同
調(diào)頭朝相反方向走。假設(shè)螞蟻們每秒鐘可以走5厘米的距離.
編程計算所以螞蟻離開細(xì)桿用的最小時間和最大時間。
我的ruby代碼如下,窮舉開始時螞蟻方向的各種組合。然后一一計算出來離開時間。
ruby-1.8.6 i386-mswin32
文件一 ant_class.rb:
class Ant
attr_reader:posi
def initialize(direction,position,speed)
##direction must be 1 or -1
@direct = direction
@posi = position
@spd = speed
@tmintval = 1
end
def getpposition()
return @posi
end
def getdirect()
if @direct == -1
return "<-"
else
return "->"
end
end
def move()
@posi += @spd*@tmintval*@direct
end
def turnback()
@direct *= -1
end
end
|
文件二 stick_class.rb:
require"ant_class"
class Stick
def initialize(length)
@sticklen = length
@Ants = Array.new()
end
def putants(directs,posits)
for i in 0...posits.size()
@Ants = Ant.new(directs,posits,5)
end
end
##因為螞蟻的移動速度是5,所以相對和相背移動兩種情況中,
##任何兩個螞蟻之間的相對速度都是10,又任何兩只螞蟻的開始距離間隔是10的倍數(shù)。
##所以螞蟻之間的距離只能是10的倍數(shù)。我們判斷碰撞的方法就簡單了
##只需要判斷坐標(biāo)相同就是了。
def anymeet()
for i in 0...@Ants.size()
if (i+1) == @Ants.size()
break
elsif (@Ants).getposition == @Ants[i+1].getposition
@Ants.turnback()
@Ants[i+1].turnback()
puts "ant"+i+"meet"+"ant"+(i+1)+"at"+ @Ants.getposition
else
print "."
end
end
end
def anyout()
for i in 0...@Ants.size()
if (@Ants).getposition >= @sticklen
@Ants.delete_at(i)
print "ant #{i} out"
end
if @Ants.getposition <= 0
@Ants.delete_at(i)
print "ant #{i} out"
end
end
end
def isallout()
if @Ants.size() == 0
return true
else
return false
end
end
def showdirects()
puts "directions:"
@Ants.each do |ant|
print ant.getdirect()+" "
end
puts ""
end
def showpositions()
puts "positions:"
@Ants.each do |ant|
print ant.posi.to_s + " "
end
end
def run()
@time = 0
self.showdirects()
self.showpositions()
while !self.isallout
@time += 1
puts @time
@Ants.each {|ant| ant.move()}
self.anymeet()
self.anyout()
end
puts "========"
puts "time = #{@time}"
end
end |
文件三 ant.rb
#####
#####
require "stick_class"
###begin
if __FILE__ == $0
directs = [-1,-1,-1,-1,-1]
positions = [30,80,110,160,250]
time = 0
st = Stick.new(300)
#I hate this
for i in 0...(2**5)
if 0x10 & i == 0
directs[0]= -1
else
directs[0] = 1
end
if 0x08 & i == 0
directs[1]= -1
else
directs[1]= 1
end
if 0x04 & i == 0
directs[2] = -1
else
directs[2] = 1
end
if 0x02 & i == 0
directs[3] = -1
else
directs[3] = 1
end
if 0x01 & i == 0
directs[4] = -1
else
directs[4] = 1
end
puts "case #{i}"
st.putants(directs,positions)
st.run()
end
end
|
我的問題:
我使用的IDE是eclipse 在執(zhí)行程序時報告如下錯誤:
./stick_class.rb:21:in `anymeet': undefined method `getposition' for #<Ant:0x2ba9058 @posi=25, @direct=-1, @tmintval=1, @spd=5> (NoMethodError)
from ./stick_class.rb:18:in `each'
from ./stick_class.rb:18:in `anymeet'
from ./stick_class.rb:77:in `run'
from D:/workspace/Ant/ant.rb:60
from D:/workspace/Ant/ant.rb:22:in `each'
from D:/workspace/Ant/ant.rb:22 |
很明顯Ant類里面有g(shù)etposition這個方法。
為什么會出現(xiàn)udefined method這種情況呢,而且在代碼別處也有過這種錯誤,都是莫名其妙的消失了。
初學(xué)ruby才幾天,求高人解答 |
|