data=[12,13,13,13,15,14,15,16,15,19] ary=Array.new(5){Array.new(6)} data.each_with_index do |v, i| a = i / 2 b = 3 + i % 2 ary[a][b] = v end p ary #输出:[[nil, nil, nil, 12, 13, nil], [nil, nil, nil, 13, 13, nil], [nil, nil, nil, 15, 14, nil], [nil, nil, nil, 15, 16, nil], [nil, nil, nil, 15, 19, nil]]
data=[12,13,13,13,15,14,15,16,15,19]
ary=Array.new(5){Array.new(6)}
data.each_with_index do |v, i|
a = i / 2
b = 3 + i % 2
ary[a][b] = v
end
p ary
#输出:[[nil, nil, nil, 12, 13, nil], [nil, nil, nil, 13, 13, nil], [nil, nil, nil, 15, 14, nil], [nil, nil, nil, 15, 16, nil], [nil, nil, nil, 15, 19, nil]]
a和b的算法:
arr = [0,1,2,3,4,5,6,7,8,9,10] p arr.map {|i| i / 2} #输出: [0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5] p arr.map {|i| 3 + i % 2} #输出: [3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4]
arr = [0,1,2,3,4,5,6,7,8,9,10]
p arr.map {|i| i / 2} #输出: [0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5]
p arr.map {|i| 3 + i % 2} #输出: [3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4]
|