Skip to content

Instantly share code, notes, and snippets.

@codian
Last active October 13, 2015 19:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codian/4243388 to your computer and use it in GitHub Desktop.
Save codian/4243388 to your computer and use it in GitHub Desktop.
아일러브커피-원하는 만큼의 원두 사용을 위해 판매해야 하는 메뉴 조합은?
class Bean
MENUS = [25, 50, 99, 210, 310, 170, 270, 590, 430, 620, 360, 450,
420, 320, 677, 308, 210, 536, 364, 466, 675, 572, 820, 959, 861,
1060, 1200, 260, 483].sort
attr_accessor :remain, :menus
def initialize(num)
if num.is_a?(Bean)
@remain = num.remain
@menus = num.menus.clone
else
@remain = num
@menus = []
end
end
def subtractable?(num)
remain >= MENUS[0] && remain - num >= 0
end
def subtract(num)
unless subtractable?(num)
raise ArgumentError.new("Can't subtract #{num} from #{@remain}")
end
@remain = @remain - num
@menus << num
true
end
def complete?
@remain == 0 && @menus.length > 0
end
def to_s
"<#{@remain}, #{@menus}>"
end
def self.sell(num)
Bean::MENUS.reverse_each do |menu|
new_num = Bean.new(num)
if new_num.subtractable?(menu)
new_num.subtract(menu)
if new_num.complete?
puts "COMPLETE: " + new_num.to_s
return true
else
return true if sell(new_num)
end
end
end
false
end
end
# 사용해야 하는 원두가 2518 이라면
Bean.sell(Bean.new(2518))
@sublee
Copy link

sublee commented Dec 9, 2012

ㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋ

@hoyajigi
Copy link

ㅋㅋㅋㅋㅋ

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment