计算瓶酒的数量

public class SimpleConstructor{
	
	//啤酒2元一瓶,四个瓶盖可换一瓶啤酒,2个空瓶也可换一瓶啤酒,10元最多可以喝多少瓶啤酒?
	static int beerPrice=2;	//啤酒的价格
	static int price=10;	//10元
	static int count=0;		//总瓶数
	final static int EXCHANGE=2;		//空瓶兑换
	final static int EXCHANGECOVER=4;	//瓶盖兑换
	public static void main(String[] args){
		int beerBody=price/beerPrice;	//临时瓶数
		count=beerBody;
		getCount(beerBody,EXCHANGE);
		getCount(beerBody,EXCHANGECOVER);
		System.out.print(count);
		
	}
	
	/**
	 * 计算兑换的瓶数(使用递归算法)
	 * @param beerBody 瓶数
	 * @param exchange 兑换类型
	 * @return
	 */
	public static int getCount(int beerBody,int exchange)
	{	
		if(beerBody<exchange){
			return 1;
		}else
		{
			count++;
			beerBody=beerBody-exchange+1;
			return getCount(beerBody,exchange);
		}
	}
}

 

posted on 2015-10-29 16:21  Ironape  阅读(303)  评论(0编辑  收藏  举报

导航