喝酒问题:啤酒2元一瓶 4个瓶盖能换1瓶啤酒,2个空瓶也能换1瓶啤酒

今天被同事问到一个喝酒的问题,题目如下:

啤酒2元一瓶 4个瓶盖能换1瓶啤酒,2个空瓶也能换1瓶啤酒,问:10元钱最多能喝几瓶酒?


于是乎写了一段小代码来解决这个问题:


package test2;

import org.junit.Test;

public class MyTest {

	int maxCount = 0;

	@Test
	public void test01() throws Exception {
		recursion(10, 0, 0, 0);
		System.out.println("总数:" + maxCount);
	}

	public void recursion(int money, int cap, int bottle, int count) {
		if (cap < 4 && bottle < 2 && money < 2) {
			if (maxCount < count) {
				maxCount = count;
			}
			return;
		}
		if (cap >= 4) {
			recursion(money, cap - 4 + 1, bottle + 1, count + 1);
		}
		if (bottle >= 2) {
			recursion(money, cap + 1, bottle - 2 + 1, count + 1);
		}
		if (money >= 2) {
			recursion(money - 2, cap + 1, bottle + 1, count + 1);
		}

		return;

	}

}


结果是15种.但是百度之后发现有人说是20种.

从实际操作上来说15种已经是最多了.

比较坑爹..........

算是回顾一下递归...


  • 5
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值