您当前的位置: 首页 >  ruby

ruby解决猴子大王问题

发布时间:2010-06-07 15:55:00 ,浏览量:0

class Ring def initialize(n=10) @array = (1..n).to_a @pos = 0 end def next_del raise "just left one! #{self.to_s}" if @array.size == 1 @pos = 0 if @pos == @array.size if yield(@array[@pos]);@array.delete_at(@pos) else @pos+=1 end;nil end def to_s "#{@array}" end end M,N=20,7 begin i = 1 r = Ring.new M r.next_del do |x| is_del = false (puts r.to_s;is_del=true) if i%N == 0 i+=1;is_del end while true rescue => ex puts ex.message end

关注
打赏
1688896170
查看更多评论

暂无认证

  • 0浏览

    0关注

    107766博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文
立即登录/注册

微信扫码登录

0.2726s