Debian编译Ruby

1、首先要安装两个依赖项

#apt-get install zlib1g zlib1g-dev
#apt-get install libssl libssl-dev

2、然后下载Ruby源码
https://www.ruby-lang.org/en/

./configure --prefix=usr/ruby/ruby_2.2.3
make
make install

3、配置环境变量
编辑/etc/profile

export RUBY_HOME=/usr/ruby/ruby_2.2.3
export RUBYLIB=$RUBY_HOME/lib/ruby/2.2.0:$RUBY_HOME/lib/ruby/2.2.0/x86_64-linux:$RUBY_HOME/share/ri/2.2.0:$RUBYLIB
export PATH=$RUBY_HOME/bin:$PATH

更新环境变量

source /etc/profile

4、切换为国内源

gem sources --add https://ruby.taobao.org/
gem sources --remove https://rubygems.org/
gem sources -l

5、安装rails

$ gem install rails

八后问题Ruby02

Queen.rb

class Queen
  
  def initialize()
    @v=0
  end
  
  def canSetQueen(lst,s,x,y)
      lstNo = Array.new()
    
      for i in 0..s-1 do
        p = Pt.new(x,i)
        lstNo.push(p)
      end
      
      for j in 0..s-1 do
        p = Pt.new(j,y)
        lstNo.push(p)
      end 
      
      x0=x
      y0=y
      while x0<s and y0<s
        p=Pt.new(x0,y0)
        lstNo.push(p)
        x0=x0+1
        y0=y0+1
      end
      
      x0=x
      y0=y
      while x0>=0 and y0>=0
        p=Pt.new(x0,y0)
        lstNo.push(p)
        x0=x0-1
        y0=y0-1
      end
      
      x0=x
      y0=y
      while x0>=0 and y0<s
        p=Pt.new(x0,y0)
        lstNo.push(p)
        x0=x0-1
        y0=y0+1
      end
      
      x0=x
      y0=y
      while x0<s and y0>=0
        p=Pt.new(x0,y0)
        lstNo.push(p)
        x0=x0+1
        y0=y0-1
      end
      
      while(lstNo.length>0)
        p=lstNo.pop
        if(p.y==lst[p.x])
          return false
        end
      end
      
      return true
  end
    
  def findQueen(lst,s,x) 
      
      if(x>=s)
        @v=@v+1
        puts("]>>solution No."+@v.to_s)
        pp lst
        return
      end
      
      for y in 0..s-1 do
        if(canSetQueen(lst,s,x,y))
          lst.push(y)   
          findQueen(lst,s,x+1)
          lst.pop
        end
      end
       
    end
end

class Pt
  attr_accessor:x
  attr_accessor:y
  def initialize(x,y)
      @x=x
      @y=y
  end
end

test.rb

require "pp"
require "./Queen.rb"

len = 8
lst = Array.new()

q=Queen1.new
q.findQueen(lst,len,0)

puts("end")

八后问题Ruby01

Queen.rb

class Queen
  
  def initialize()
    @v=0
  end
  
  def arrCopy2(arr)
    arr0=Array.new(arr.length)
    for i in 0..arr.length-1 do
      arr0[i]=Array.new(arr[i])
    end
    
    return arr0
  end
  
  def addQueen(arr,x,y)
    setQueen(arr,x,y,1)
  end
  
  def setQueen(arr,x,y,n)
    for i in 0..arr.length-1 do
      arr[x][i]=n
    end
    
    for j in 0..arr[x].length-1 do
      arr[j][y]=n
    end 
    
    x0=x
    y0=y
    while x0<arr.length and y0<arr&#91;x0&#93;.length
      arr&#91;x0&#93;&#91;y0&#93;=n
      x0=x0+1
      y0=y0+1
    end
    
    x0=x
    y0=y
    while x0>=0 and y0>=0
      arr[x0][y0]=n
      x0=x0-1
      y0=y0-1
    end
    
    x0=x
    y0=y
    while x0>=0 and y0<arr&#91;x0&#93;.length
      arr&#91;x0&#93;&#91;y0&#93;=n
      x0=x0-1
      y0=y0+1
    end
    
    x0=x
    y0=y
    while x0<arr.length and y0>=0
      arr[x0][y0]=n
      x0=x0+1
      y0=y0-1
    end
    
    return arr
  end
  
  def findQueen(arr,lst,x) 
    
    if(x>=arr.length)
      @v=@v+1
      puts("]>>solution No."+@v.to_s)
      pp lst
      return
    end
    
    for y in 0..arr[x].length-1 do
      if(arr[x][y]==0)
        lst.push(y)
        arr0=arrCopy2(arr)
        addQueen(arr,x,y)   
        findQueen(arr,lst,x+1)
        arr=arr0
        lst.pop
      end
    end
     
  end
  
end

test.rb

require "pp"
require "./Queen.rb"

len = 8
arr=Array.new(len){Array.new(len, 0)}
#fuck
#arr = Array.new(len, Array.new(len,0))

lst = Array.new()

q=Queen.new
q.findQueen(arr,lst,0)

puts("end")