酒瓶和啤酒的问题

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/jinhuoxingkong/article/details/77757670

一、题目



每瓶啤酒2元,2个空酒瓶或4个瓶盖可换1瓶啤酒。10元最多可喝多少瓶啤酒?



二、程序


package test;
public class Solution {
	public static int all = 5;	
	public static int leftBottle;	
	public static int leftTop;	
	public static void main(String[] args) {		
		all = all + drink(5,5);		
		System.out.println("总共能喝"+all);		
		System.out.println("剩下酒瓶个数"+leftBottle);		
		System.out.println("剩下盖子个数"+leftTop);	
	}	
	public static int drink(int bottle, int top) {		
		if(bottle >=2 || top >=4){			
			leftBottle = (bottle/2)+(bottle%2)+(top/4);			
			leftTop = (top/4) + (top%4) + (bottle/2);			
			return  (bottle/2) + (top/4) + drink(leftBottle,leftTop);		
		}		
		return 0; 	
	}
}

执行结果如下

总共能喝15
剩下酒瓶个数1
剩下盖子个数3






阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页