其他分享
首页 > 其他分享> > 啤酒2块1瓶, 4个盖换1瓶, 2个空瓶换1瓶, 10块可以喝几瓶?

啤酒2块1瓶, 4个盖换1瓶, 2个空瓶换1瓶, 10块可以喝几瓶?

作者:互联网

public class Calculation {


    public static void main(String arg[]){
        int beer=calculationBeerAmount(10,2,4,2);
        System.out.println("10元共喝了" + beer + "瓶啤酒.");

    }

    /**
     * 计算啤酒数量
     * @param money 金额
     * @param unitPrice 每瓶酒单价
     * @param capRule 瓶盖兑换规则
     * @param bottelRule 瓶子规则兑换
     * @return
     */
    public static int calculationBeerAmount(int money,int unitPrice,int capRule,int bottelRule){
        int beer = money / unitPrice;//初始的啤酒数
        int bottel = beer;//初始的瓶子数
        int cap = beer;//初始的瓶盖数
        int temp;
        while (cap >= capRule || bottel >= bottelRule) {
            if ((temp = cap / capRule) > 0) {
                beer = beer + temp;
                cap = cap - capRule * temp + temp;
                bottel = bottel + temp;
            }
            if ((temp = bottel / bottelRule) > 0) {
                beer = beer + temp;
                cap = cap + temp;
                bottel = bottel - bottelRule * temp + temp;
            }
        }
        return beer;
    }
}

标签:bottel,10,capRule,temp,int,cap,beer,个盖换,空瓶
来源: https://blog.csdn.net/qq_42093488/article/details/88856255