算法题:买汽水(瓶子瓶盖换水)
题目:1元一瓶水,两个瓶子可以换1瓶水,三个瓶盖可以换一瓶水,问,20元可以喝到多少瓶水
/**
* 题目:
* 1元一瓶水,两个瓶子可以换1瓶水,三个瓶盖可以换一瓶水,问,20元可以喝到多少瓶水
*/
public class Test {
public static void main(String[] args) {
int money = 20;
System.out.println("买水:"+money+ "元");
int bottleWater = money + getBottleWater(money, money);
System.out.println("一共可以喝水 "+bottleWater+" 瓶");
}
/**
* 用瓶子和瓶盖换水
* @param container 瓶子(容器)
* @param cover 瓶盖
* @return 用材料换到水的个数
*/
private static int getBottleWater(int container , int cover){
int water = container/2 + cover/3;
container = container%2;
cover = cover%3;
if (water == 0){
return water;
}
return water+getBottleWater(container+water,cover+water);
}
}
结果: