递归算法:猴子吃桃

public class MonkeyAndPeach {

	/**
	 * 		猴子吃桃问题:猴子第一天摘下若干个桃子,当即吃了一半,还不过瘾,又多吃了一个。
	 * 第二天又将剩下的桃子吃掉一半,又多吃了一个。以后每天都吃了前一天剩下的一半零一个。
	 * 到第10天,只剩下一个桃子了。试求第一天共摘多少桃子?
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		System.out.println(Compute(10));
	}

	public static int Compute(int n) {
		if (n == 1)
			return 1;
		else
			//再次调用
			return 2 * Compute(n - 1) + 2;
	}
}

猜你喜欢

转载自xiongjiajia.iteye.com/blog/1742798