10元买汽水,2个瓶子换一瓶,4个盖子换一瓶,做多能喝多少瓶

public static void main(String[] args) {
        System.out.println(buyWater(10));
    }
    static int buyWater(int money){        
        int count=money/2;
        int bo=count;
        int cap=count;        
        while(bo>=2||cap>=4){
                while(bo>=2){
                    count++;
                    //换一瓶,获得一个瓶子,扣除两个瓶子
                    bo=bo+1-2;
                    cap++;
                }
                while(cap>=4){
                    count++;
                    //换一瓶,获得一个瓶盖,扣除4个瓶盖
                    cap=cap+1-4;
                    bo++;
                }                
            }
        
        return count;
        
    }

 最后结果:15瓶

总结:要用while循环把瓶子盖子消耗完,单步调试发现,不够15瓶的情况都是没有消耗完瓶子盖子。

猜你喜欢

转载自www.cnblogs.com/findlisa/p/11436648.html