1元钱买一瓶汽水,2个空瓶换一瓶汽水,3个瓶盖换一瓶汽水,问:3块钱能和多少瓶汽水?

问题:

1元钱买一瓶汽水,2个空瓶换一瓶汽水,3个瓶盖换一瓶汽水,问:3块钱能和多少瓶汽水?

代码实现:

public class DrinkWater {
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		System.out.print("请输入金额:");
		int money = scan.nextInt(); //金额
		int bottle = money; //空瓶
		int cap = money; //瓶盖
		int sum = money; //可以喝的汽水的总瓶数
		System.out.println(money+"元直接买的瓶数:"+sum);
		while(true){
			//换取的饮料瓶数
			int changeBottle = bottle/2;
			int changeCap = cap/3;
			int change = changeBottle + changeCap;
			//换取后剩下
			int balBottle = bottle%2;
			int balCap = cap%3;
			//当前已经喝的总瓶数
			sum = sum + change;
			//饮料喝完后的总的空瓶数喝瓶盖数
			bottle = balBottle + change;
			cap = balCap + change;
			
			if(bottle<2 && cap <3){
				break;
			}
		}
		System.out.println(money+"元可以喝到的总的瓶数:"+sum);
		scan.close();
		
	}
}

 
 
 
 
 
 
 

你可能感兴趣的:(java中的一些简单算法)