某酒吧搞啤酒促销,啤酒2元一瓶,4个瓶盖可以换一瓶啤酒,两个空瓶也可以换一瓶啤酒,现有10元钱,在不允许找别人借酒瓶或瓶盖的情况下,问一共可以喝多少瓶啤酒,还剩多少瓶子和盖子?
public class BeerDemo {
public static int totalNums;
public static int lastPingZiNums;
public static int lastGaiZiNums;
public static void main(String[] args) {
buyBeer(10);
System.out.println("总共啤酒的数量" + totalNums);
System.out.println("剩余瓶子的数量" + lastPingZiNums);
System.out.println("剩余盖子的数量" + lastGaiZiNums);
}
public static void buyBeer(int money) {
int totalMoney = 0;
int number = money / 2;
totalNums += number;
//计算当前瓶子和盖子的数量
int currentPingZiNums = lastPingZiNums + number;
int currentGaiZiNums = lastGaiZiNums + number;
//计算现在总共有多少钱
totalMoney += (currentPingZiNums / 2) * 2;
totalMoney += (currentGaiZiNums / 4) * 2;
//计算出剩余的瓶子数量
lastPingZiNums = currentPingZiNums % 2;
//剩余盖子的数量
lastGaiZiNums = currentGaiZiNums % 4;
if (totalMoney >= 2) {
buyBeer(totalMoney);
}
}
}
结果: