返回题解分享
讨论 / 题解分享/ 帖子详情

李白打酒(结果填空) - 题解

随便写的,答案正确了,不知道对不对

public class Main {
	public static void main(String[] args) {
		// 从结尾出发
		// 最后一次是喝酒(b)
		int res = sum(0, 1, 1);
		System.out.println(res);
	}

	/**
	 * 
	 * @param a   遇店次数
	 * @param b   遇花次数
	 * @param res 酒壶剩余酒量
	 * @return
	 */

	static int sum(int a, int b, int rest) {
		if (a == 5 && b == 10 && rest == 2) {// 终止条件
			return 1;
		}
		int ans = 0;
		if (a + 1 <= 5 && rest % 2 == 0) {
			// 买酒
			ans += sum(a + 1, b, rest / 2);
		}
		if (b + 1 <= 10) {
			//喝酒
			ans += sum(a, b + 1, rest + 1);
		}
		return ans;
	}
}
0 回复 0 转发 1 喜欢 1 阅读
回复 (0)
默认 最新
暂无回复,快来抢沙发!