`
yujianlong1988
  • 浏览: 7047 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

写下我第一个博客 1元钱一瓶汽水,喝完后两个空瓶换一瓶汽水,问:你有20元钱,最多可以喝到几瓶汽水?

 
阅读更多
public class Test {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		//20块钱,1块钱买一瓶饮料,2个瓶子换一个饮料的情况

		Test tt = new Test("1_1", 2, 20);
		tt.compute();

		//39块钱,4块钱买3瓶饮料,5个瓶子换1个饮料的情况
		Test tt2 = new Test("4_3", 5, 39);
		tt2.compute();

		//System.out.println("======扩展===39块钱,4块钱买3瓶饮料,5个瓶子换3个饮料的情况======未实现===");
	}

	public Test(String money_count, int bottleCount, int moneyhave) {
		super();
		this.money_count = money_count;
		this.bottleCount = bottleCount;
		this.moneyhave = moneyhave;
	}

	/**
	 * 4块钱换3个饮料
	 * 2个饮料瓶子换1个饮料
	 * 现在有30块钱
	 */

	/**
	 * 多少钱换多少瓶饮料
	 * 比如4块钱换3个饮料
	 */
	private String money_count = "";
	/**
	 * 多少个瓶子换一瓶饮料
	 */
	private int bottleCount = 0;

	/**
	 * 你现在拥有多少钱
	 */
	private int moneyhave;

	/**
	 * service方法,计算你拥有的钱一共可以换多少饮料
	 * @param moneyhave
	 * @param money
	 * @param count
	 */
	public void compute() {
		String[] moneyAndCount = this.getMoney_count().split("_");
		int count = Integer.valueOf(moneyAndCount[1]).intValue();
		int money = Integer.valueOf(moneyAndCount[0]).intValue();
		int initcount = this.getMoneyhave() * count / money;
		int leaveMoney = this.getMoneyhave() * count % money;

		initcount += bottle(initcount, this.getBottleCount());
		System.out.println("=========" + this.getMoneyhave() + "块钱," + money + "块钱买" + count + "瓶饮料,"
				+ this.getBottleCount() + "个瓶子换一个饮料的情况=========");
		System.out.println("你一共可以喝多少瓶饮料" + initcount);
		System.out.println("你还剩多少钱" + leaveMoney);

	}

	/**
	 * 算多少个瓶子换多少瓶饮料
	 * @param bottle
	 * @param bottleCount
	 * @return
	 */
	public int bottle(int bottle, int bottleCount) {
		if (bottle > 1) {
			int nowbottle = bottle / bottleCount + bottle % bottleCount;
			return bottle / bottleCount + bottle(nowbottle, bottleCount);

		} else {
			return 0;
		}

	}

	public String getMoney_count() {
		return money_count;
	}

	public void setMoney_count(String money_count) {
		this.money_count = money_count;
	}

	public int getBottleCount() {
		return bottleCount;
	}

	public void setBottleCount(int bottleCount) {
		this.bottleCount = bottleCount;
	}

	public int getMoneyhave() {
		return moneyhave;
	}

	public void setMoneyhave(int moneyhave) {
		this.moneyhave = moneyhave;
	}
}

运行结果

=========20块钱,1块钱买1瓶饮料,2个瓶子换一个饮料的情况=========
你一共可以喝多少瓶饮料39
你还剩多少钱0
=========39块钱,4块钱买3瓶饮料,5个瓶子换一个饮料的情况=========
你一共可以喝多少瓶饮料36
你还剩多少钱1

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics