2元钱1瓶,4个瓶盖换一瓶,2个空瓶换一瓶,问:10元钱可以喝几瓶
public class TestDemo {
public static void main(String[] args) {
int money = 10;//钱
int beer = 0;// 啤酒
int enpty = 0;//空瓶
int cover = 0;//盖子
for(; money>=2; money-=2)//规则一~2元钱1瓶:只要钱大于等于2块就还可以买酒
{
enpty++;
cover++;
beer++;
}
while(enpty>=2 || cover>=4){
for(;enpty>=2; enpty-=2)//规则二~2个空瓶换一瓶:只要空瓶的数目大于等于2就还可以买酒
{
enpty++;
cover++;
beer++;
}
for(;cover>=4; cover-=4)//规则三~4个瓶盖换一瓶:只要瓶盖的数目大于等于4就可以买酒
{
enpty++;
cover++;
beer++;
}
}
System.out.println("10块大洋可以喝"+beer+"瓶啤酒,"+"还剩"+money+"块大洋,"+"还剩"+cover+"瓶盖,"+"还剩"+enpty+"空瓶");
}
}