zoukankan      html  css  js  c++  java
  • 计算瓶酒的数量

    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);
    		}
    	}
    }
    
  • 相关阅读:
    java 读取src下的配置文件
    图书管理系统
    StaticDemo
    MatixDemo
    Magics
    GetPrime
    ConWithThis
    TestTHIS
    ConstructorOverLoad
    Sequence
  • 原文地址:https://www.cnblogs.com/guyg/p/4920876.html
Copyright © 2011-2022 走看看