卖酒的算法题解

• `afternoon = morning * 2`

``````irb(main):005:0> arr.permutation(2).to_a
=> [[1, 2], [1, 3], [2, 1], [2, 3], [3, 1], [3, 2]]
``````

``````irb(main):006:0> buckets = [30, 32, 36, 38, 40, 62]
=> [30, 32, 36, 38, 40, 62]
``````

``````irb(main):012:0> buckets.combination(2).to_a
=> [[30, 32], [30, 36], [30, 38], [30, 40], [30, 62], [32, 36], [32, 38], [32, 40], [32, 62], [36, 38], [36, 40], [36, 62], [38, 40], [38, 62], [40, 62]]
``````

``````buckets = [30, 32, 36, 38, 40, 62]
sold_at_morning = buckets.combination(2).to_a

arr = []
for x in sold_at_morning
left = buckets - x
sold_at_afternoon = left.combination(3).to_a
sum_morning = x.inject(0) { |sum, _x| sum + _x }
for y in sold_at_afternoon
sum_afternoon = y.inject(0) { |sum, _y| sum + _y }
if sum_afternoon.to_f / sum_morning == 2
puts "morning: #{x} / afternoon: #{y} / red wine: #{buckets - x - y}"
exit
end
end
end
``````

``````sum_morning = x.inject(0) { |sum, _x| sum + _x }
``````

``````sum_afternoon.to_f / sum_morning == 2
``````

``````morning: [30, 36] / afternoon: [32, 38, 62] / red wine: [40]
``````

My Github Page: https://github.com/liweinan