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

看板Gossiping作者 (赤迷迭)時間7年前 (2018/04/17 01:30), 7年前編輯推噓6(606)
留言12則, 12人參與, 7年前最新討論串16/18 (看更多)
八卦黑客松 Run 起來! #!/usr/bin/env ruby # 輸入 input = [1, 4, 6] # 計算特徵 def calc_pattern(input) # 把 input 每一個都拿來當指數乘,算總合。 input.inject(0) { |sum, exp| sum + 2**(exp - 1) } end # 旋轉演算法 def circle_shift(input, max = 6) target = input output = [] max.times do # 把尾巴加到頭。 target = target / 2 + (target % 2) * 2**(max - 1) # 收集這些計算後的整數。 output << target end output end # 餘數分整數 def separate(input) output = [] cursor = 1 target = input until target < 1 output << cursor if target.odd? 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] -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 111.250.246.160 ※ 文章網址: https://www.ptt.cc/bbs/Gossiping/M.1523899847.A.D07.html

04/17 01:31, 7年前 , 1F
我應該去念電資的:3
04/17 01:31, 1F

04/17 01:31, 7年前 , 2F
看不懂喇
04/17 01:31, 2F

04/17 01:32, 7年前 , 3F
不愧是111的 比112還強
04/17 01:32, 3F

04/17 01:32, 7年前 , 4F
Ruby喔?Linux可以跑喔?
04/17 01:32, 4F

04/17 01:34, 7年前 , 5F
linux當然可以跑ruby
04/17 01:34, 5F

04/17 01:34, 7年前 , 6F
作業做起來!!
04/17 01:34, 6F

04/17 01:35, 7年前 , 7F
組語的寫法呢
04/17 01:35, 7F

04/17 01:44, 7年前 , 8F
有個問題就是 [1, 3, 5]、[2, 4, 6] 的組合我沒 uniq。
04/17 01:44, 8F

04/17 01:48, 7年前 , 9F
有C++或Java版本嗎
04/17 01:48, 9F

04/17 02:00, 7年前 , 10F
我想要 C 版本的...
04/17 02:00, 10F

04/17 03:15, 7年前 , 11F
C不就指標掛上去就好ㄌㄇ...
04/17 03:15, 11F

04/17 03:28, 7年前 , 12F
有pascal版的嗎?
04/17 03:28, 12F
※ 編輯: zero00072 (118.163.133.244), 04/17/2018 11:59:48 ※ 編輯: zero00072 (118.163.133.244), 04/18/2018 10:11:26 ※ 編輯: zero00072 (118.163.133.244), 06/15/2018 13:16:24
文章代碼(AID): #1QrDt7q7 (Gossiping)
討論串 (同標題文章)
文章代碼(AID): #1QrDt7q7 (Gossiping)