閱讀497 返回首頁    go 阿裏雲 go 技術社區[雲棲]


ruby解決猴子大王問題

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

 

 

最後更新:2017-04-02 06:51:17

  上一篇:go magento 1.4-- 新功能Widget使用初步示例
  下一篇:go magento -- 前台新建block的小技巧