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

 

 

相关文章
|
3月前
|
Ruby
|
3月前
|
Ruby
|
2月前
|
数据采集 Web App开发 数据处理
Ruby网络爬虫教程:从入门到精通下载图片
Ruby网络爬虫教程:从入门到精通下载图片
|
3月前
|
JSON 数据格式 Ruby
|
3月前
|
JSON Ubuntu Linux
|
3月前
|
存储 JSON 数据格式
|
3月前
|
安全 Ruby
|
3月前
|
调度 Ruby
|
3月前
|
人工智能 BI 计算机视觉
|
3月前
|
Ruby