编程解决买啤酒问题

问题如下:

编程解决买啤酒问题_第1张图片

解决方案(lua代码):

local money = 10
local bottle = 0
local cover = 0
local drink = 0

while (money >= 2 or bottle >= 2 or cover >= 4) do
    local haveDrink = false
	if bottle >= 2 and haveDrink == false then
		bottle = bottle - 2 + 1
		cover = cover + 1
		drink = drink + 1
		haveDrink = true
	end
	if cover >= 4 and haveDrink == false then
		cover = cover - 4 + 1
		bottle = bottle + 1
		drink = drink + 1
		haveDrink = true
	end
	if money >= 2 and haveDrink == false then
		money = money - 2
		bottle = bottle + 1
		cover = cover + 1
		drink = drink + 1
		haveDrink = true
	end
	print(drink)
end


你可能感兴趣的:(算法)