赞 | 23 |
VIP | 22 |
好人卡 | 18 |
积分 | 608 |
经验 | 44466 |
最后登录 | 2024-11-9 |
在线时间 | 1934 小时 |
Lv6.析梦学徒 Fuzzy Ginkgo Taciturn Knight
- 梦石
- 0
- 星屑
- 60809
- 在线时间
- 1934 小时
- 注册时间
- 2010-6-26
- 帖子
- 1605
|
本帖最后由 orzfly 于 2012-1-25 22:19 编辑
- def possible?(array)
- dims = Math.sqrt(array.length).to_i
- return nil if dims ** 2 != array.length
- eq1 = inversion_number(array) % 2 == 0
- return eq1 if dims % 2 == 1
- eq2 = (array.index(0) / dims - (dims - 1)) % 2 == 0
- return (!eq1) == (eq2)
- end
- def inversion_number(array)
- t = 0; (array.length - 1).times { |x| array[0, x + 1].each { |y| t += 1 if y < array[x + 1] and y != 0 } }; t
- end
- print "#{possible?([1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,0])}\n"
- print "#{possible?([1,2,3,4,5,6,7,8,9,10,11,0,12,13,14,15])}\n"
- print "#{possible?([1,2,3,4,0,5,6,7,9,10,11,8,13,14,15,12])}\n"
- print "#{possible?([1,2,3,4,5,6,7,8,0])}\n"
- print "#{possible?([1,2,3,4,5,6,7,0,8])}\n"
- print "#{possible?([2,1,3,4,5,6,7,8,0])}\n"
复制代码 参考资料 = = http://blog.csdn.net/tiaotiaoyly/article/details/2008233
我只是写了个ruby版而已@各种压力的猫君
打乱数组 |
|