酒吧不可以借酒
酒吧可以借酒
//
// main.swift
// beerMaxDrink
//
// Created by on 2020/2/4.
// Copyright © 2020. All rights reserved.
//
/*
酒吧啤酒2元一瓶,两个空瓶或四个瓶盖可以换一瓶啤酒(酒吧概不借酒),你有10元钱,请问最多可以喝几瓶?
*/
import Foundation
//共累计喝的瓶数
var drinkSum = 0
//当前瓶子数
var bottleNum = 0
//当前瓶盖数
var capsNum = 0
//返回(累计喝瓶数,剩余瓶子数,剩余瓶盖数)
func getMaxDrinkSum(money:Float,price:Float) ->(Int,Int,Int) {
bottleNum = Int(money / price)
capsNum = bottleNum
drinkSum = bottleNum
//开始换酒,是个循环
while bottleNum > 1 || capsNum > 3 {
//酒瓶换
let wineAddedByBottle = bottleNum / 2
drinkSum += wineAddedByBottle
bottleNum = bottleNum % 2 + wineAddedByBottle
capsNum += wineAddedByBottle
//酒盖换
let wineAddedByCaps = capsNum / 4
drinkSum += wineAddedByCaps
capsNum = capsNum % 4 + wineAddedByCaps
bottleNum += wineAddedByCaps
}
return (drinkSum,bottleNum,capsNum)
}
//可以借酒时,返回(累计喝瓶数,剩余瓶子数,剩余瓶盖数)
func getMaxDrinkSumCanBorrow(money:Float,Int) {
var borrowedNum = 0
bottleNum = Int(money / price)
capsNum = bottleNum
drinkSum = bottleNum
//开始换酒,是个循环
while bottleNum >= 1 || capsNum >= 3 {
//酒瓶换
let wineAddedByBottle = bottleNum / 2
drinkSum += wineAddedByBottle
bottleNum = bottleNum % 2 + wineAddedByBottle
capsNum += wineAddedByBottle
//如果(2酒瓶2酒盖时,"酒瓶换"已经把2酒瓶换为1瓶1盖,即变为1瓶3盖)借一瓶后,空瓶和瓶盖能换的酒数 <= 已借瓶数时,结束;否则,借一瓶
if 1 == bottleNum || 3 == capsNum{//不能少,否则在循环时会提前借酒
if (bottleNum + 1)/2 + (capsNum + 1)/4 <= borrowedNum {
//不应该喝一瓶,回退
bottleNum += 1
capsNum -= 1
return (drinkSum - 1,bottleNum - borrowedNum,capsNum - borrowedNum)
}else{ //借一瓶
borrowedNum += 1
bottleNum += 1
capsNum += 1
//酒瓶换
let wineAddedByBottle = bottleNum / 2
drinkSum += wineAddedByBottle
bottleNum = bottleNum % 2 + wineAddedByBottle
capsNum += wineAddedByBottle
}
}
//酒盖换
let wineAddedByCaps = capsNum / 4
drinkSum += wineAddedByCaps
capsNum = capsNum % 4 + wineAddedByCaps
bottleNum += wineAddedByCaps
}
return (drinkSum,capsNum)
}
for i in 0..<20{
let rslt0 = getMaxDrinkSum(money: Float(i),price: 2)
print("-----------------------")
print("\(i)元:不能借酒:\(rslt0)")
let rsltCanBorrow0 = getMaxDrinkSumCanBorrow(money: Float(i),price: 2)
print("能借酒:\(rsltCanBorrow0)")
}
let rslt1 = getMaxDrinkSum(money: 10.0,price: 5)
print("-----------------------")
print("酒价格5元时,不能借酒:\(rslt1)")
let rsltCanBorrow1 = getMaxDrinkSumCanBorrow(money: 10.0,price: 5)
print("能借酒:\(rsltCanBorrow1)")
-----------------------
0元:不能借酒:(0,0)
能借酒:(0,0)
-----------------------
1元:不能借酒:(0,0)
-----------------------
2元:不能借酒:(1,1,1)
能借酒:(4,0)
-----------------------
3元:不能借酒:(1,0)
-----------------------
4元:不能借酒:(3,3)
能借酒:(8,0)
-----------------------
5元:不能借酒:(3,0)
-----------------------
6元:不能借酒:(7,3)
能借酒:(12,0)
-----------------------
7元:不能借酒:(7,0)
-----------------------
8元:不能借酒:(11,3)
能借酒:(16,0)
-----------------------
9元:不能借酒:(11,0)
-----------------------
10元:不能借酒:(15,3)
能借酒:(20,0)
-----------------------
11元:不能借酒:(15,0)
-----------------------
12元:不能借酒:(19,3)
能借酒:(24,0)
-----------------------
13元:不能借酒:(19,0)
-----------------------
14元:不能借酒:(23,3)
能借酒:(28,0)
-----------------------
15元:不能借酒:(23,0)
-----------------------
16元:不能借酒:(27,3)
能借酒:(32,0)
-----------------------
17元:不能借酒:(27,0)
-----------------------
18元:不能借酒:(31,3)
能借酒:(36,0)
-----------------------
19元:不能借酒:(31,0)
-----------------------
酒价格5元时,不能借酒:(3,0)
Program ended with exit code: 0