Re: [問卦] 這段code要怎麼打?

作者: zero00072 (赤迷迭)   2018-04-17 01:30:44
八卦黑客松 Run 起來!
#!/usr/bin/ruby
# 輸入
input = [1, 4, 6]
# 計算特徵
def calc_pattern(input)
sum = 0
input.each do |exp|
# 把 input 每一個都拿來當指數乘,算總合。
sum += 2 ** ((exp - 1) % 6)
end
sum
end
# 旋轉演算法
def circle_shift(input, max=6)
target = input
output = []
max.times do |time|
# 往左推一下。
target <<= 1
# 找頭 bit。
top_bit = (2 ** max & target) / (2 ** max)
# 頭 bit 加回去推過的數字。
target += top_bit
# 然後再砍頭。
target -= 2 ** max * top_bit
# 收集這些計算後的整數。
output << target
end
output
end
# 餘數分整數
def separate(input)
output = []
cursor = 1
target = input
until target < 1
output << cursor if target % 2 == 1
cursor += 1
target /= 2
end
output
end
# 執行程式並收集未排序的列表。
unsorted = []
circle_shift(calc_pattern(input)).each do |set|
unsorted << separate(set)
end
# 不要計較排序演算法了!直接從後面排回前面。
unsorted.sort_by {|array| array[2]}
.sort_by {|array| array[1]}
.sort_by {|array| array[0]}.each do |set|
p set
end
# 執行結果:
#=> [1, 2, 5]
#=> [1, 3, 4]
#=> [1, 4, 6]
#=> [2, 3, 6]
#=> [2, 4, 5]
#=> [3, 5, 6]
作者: AirbusA350 (空中巴士A350XWB)   2018-04-17 01:31:00
我應該去念電資的:3
作者: pipi8696044 (methane)   2018-04-17 01:31:00
看不懂喇
作者: heavensun   2018-04-17 01:32:00
不愧是111的 比112還強
作者: johnhmj (耗呆肥羊)   2018-04-17 01:32:00
Ruby喔?Linux可以跑喔?
作者: kuninaka   2018-04-17 01:34:00
linux當然可以跑ruby
作者: t81511270 (justlikeING)   2018-04-17 01:34:00
作業做起來!!
作者: fezexp9987   2018-04-17 01:35:00
組語的寫法呢
作者: zero00072 (赤迷迭)   2018-04-17 01:44:00
有個問題就是 [1, 3, 5]、[2, 4, 6] 的組合我沒 uniq。
作者: ykes60513 (いちご)   2018-04-17 01:48:00
有C++或Java版本嗎
作者: zzzz8931 (肥宅)   2018-04-17 02:00:00
我想要 C 版本的...
作者: wuyiulin (龍破壞劍士-巴斯達布雷達)   2018-04-17 03:15:00
C不就指標掛上去就好ㄌㄇ...
作者: bamlien   2018-04-17 03:28:00
有pascal版的嗎?

Links booklink

Contact Us: admin [ a t ] ucptt.com