10元最多可喝多少瓶啤酒?(不可借酒+可借酒,swift语言实现)

移动开发 作者: 2024-08-24 18:40:01
背景 《爱情公寓5》中有个剧情:每瓶啤酒2元,2个空酒瓶或4个瓶盖可换1瓶啤酒。10元最多可喝多少瓶啤酒? 脑海模拟起来的确有点费劲。心算结果是15瓶,而剧情实践居然是20瓶!结合弹幕说的酒吧可能可以

酒吧不可以借酒

酒吧可以借酒

//
//  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
原创声明
本站部分文章基于互联网的整理,我们会把真正“有用/优质”的文章整理提供给各位开发者。本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
本文链接:http://www.jiecseo.com/news/show_67965.html