Chinaunix首页 | 论坛 | 博客
  • 博客访问: 108401
  • 博文数量: 4
  • 博客积分: 2046
  • 博客等级: 大尉
  • 技术积分: 371
  • 用 户 组: 普通用户
  • 注册时间: 2006-05-08 07:02
文章分类

全部博文(4)

文章存档

2009年(2)

2008年(2)

我的朋友

分类: WINDOWS

2008-08-25 22:50:44

ror的基础是ruby,我不求对ruby有多精通但是做ror的应用至少也得熟悉ruby的基本东东吧。最近看了一段时间的ruby官网的文档,写得相当好,不过E文的啃的相当慢。光看资料也没法深入理解。于是找了个问题练练手。



为解决如下问题:


有一根300厘米的细木杆,在第30厘米、80厘米、110厘米、160厘米、250厘米这五个位置上各有一只蚂蚁。木杆很细,不能同时通过

两只蚂蚁。开始时,蚂蚁的头朝左还是朝右是任意的,它们只会朝前走或调头,但不会后退。当任意两只蚂蚁碰头时,两只蚂蚁会同

调头朝相反方向走。假设蚂蚁们每秒钟可以走5厘米的距离.

编程计算所以蚂蚁离开细杆用的最小时间和最大时间。


我的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的倍数。
##所以蚂蚁之间的距离只能是10的倍数。我们判断碰撞的方法就简单了
##只需要判断坐标相同就是了。

  def anymeet()
    for i in ()
      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 ()
      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






阅读(1170) | 评论(0) | 转发(0) |
0

上一篇:ROR 体验之一

下一篇:对白

给主人留下些什么吧!~~