drink beer

package com.ming.test;

/**
 * Drunkard
 * <p>
 * money 2 = wine 1 + bottle 1 + cap 1
 *
 * <p>
 * wine 1+bottle 1+cap 1=bottle 2
 * <p>
 * wine 1+cap 1=bottle 1
 *
 * <p>
 * wine 1+bottle 1+cap 1=cap 4
 * <p>
 * wine 1 + bottle 1 = cover 3
 *
 * <p>
 * bottle 1-cap 1=cap 3-bottle 1
 * <p>
 * Bottle 1 = Cap 2
 *
 * <p>
 * Bottle 1 = Wine 2
 * <p>
 * cover 1 = wine 1
 *
 * <p>
 * Money 2 = wine 1 + bottle 1 (wine 2) + cover 1 (wine 1) = wine 4
 *
 * <p>
 * Money 10 = Wine 20
 *
 * @author ming
 *
 */
public class Drunkard {
	// The number of beer bottles drank
	static int drinkTimes;
	// Number of remaining beers to drink
	static int beerNum;
	// remaining empty bottles
	static int bottleNum;
	// number of caps remaining
	static int coverNum;
	// Number of empty bottles needed to redeem beer
	static int bottleExNum;
	// Number of caps needed to redeem beer
	static int coverExNum;
	// beer unit price
	static int price;
	// remaining money
	static int money;

	/**
	 * Initialization parameters
	 */
	static void init() {
		// The number of beer bottles drank
		drinkTimes = 0;
		// Number of remaining beers to drink
		beerNum = 0;
		// The number of remaining empty bottles (there will always be 1 left at the end of the drink)
		bottleNum = 1;
		// Number of remaining lids (there will always be 1 left at the end of the drink)
		coverNum = 1;
		// Number of empty bottles needed to redeem beer
		bottleExNum = 2;
		// Number of caps needed to redeem beer
		coverExNum = 4;
		// beer unit price
		price = 2;
		// remaining money
		money = 10;
	}

	/**
	 * Drink it when you buy it, and then buy it after you drink it
	 */
	static void buyBeer() {
		if (money >= price) {
			money = money - price;
			beerNum ++;
			drinkBear();
			buyBeer();
		}
	}

	/**
	 * After drinking, exchange, and then drink after drinking
	 */
	static void drinkBeer() {
		if (beerNum > 0) {
			beerNum--;
			drinkTimes++;
			bottleNum++;
			coverNum++;
			exchangeBeer();
			drinkBear();
		}
	}

	/**
	 * Drink after changing, and then change after drinking
	 */
	static void exchangeBeer() {
		exchangeBeerByBottle();
		exchangeBeerByCover();
		// exchangeBeerByBottleAndCover();
		drinkBear();
	}

	/**
	 * Replace with empty bottle
	 */
	static void exchangeBeerByBottle() {
		if (bottleNum >= bottleExNum) {
			bottleNum = bottleNum - bottleExNum;
			beerNum ++;
			exchangeBeerByBottle();
		}
	}

	/**
	 * Replace with cover
	 */
	static void exchangeBeerByCover() {
		if (coverNum >= coverExNum) {
			coverNum = coverNum - coverExNum;
			beerNum ++;
			exchangeBeerByCover();
		}
	}

	/**
	 * Replace with empty bottle and cap combination
	 */
	static void exchangeBeerByBottleAndCover() {
		if ((coverExNum % 2 == 0) && (bottleExNum % 2 == 0)
				&& (coverNum >= coverExNum / 2 && bottleNum >= bottleExNum / 2)) {
			coverNum = coverNum - coverExNum / 2;
			bottleNum = bottleNum - bottleExNum / 2;
			beerNum ++;
			exchangeBeerByBottleAndCover();
		}
	}

	/**
	 * Start
	 */
	public static void start() {
		init();
		buyBeer();
	}

	public static void main(String[] args) {
		start();
                /*Only give 1 bottle on credit*/
		System.out.println("Remaining money: " + money);
		System.out.println("Number of beer bottles drunk: " + drinkTimes);
		System.out.println("Number of remaining beers: " + beerNum);
		/* Borrowed 1 empty bottle and 1 lid*/
		System.out.println("Number of empty bottles remaining: " + (bottleNum - 1));
		System.out.println("Number of covers remaining: " + (coverNum - 1));
	}

}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326166644&siteId=291194637