猴子吃桃问题:猴子第一天摘下若干个桃子,当即吃了一半,还不瘾,又多吃了一个第二天早上又将剩下的桃子吃掉一半,又多吃了一个。以后每天早上都吃了前一天剩下的一半零一个。到第10天早上想再吃时,见只剩下一个桃子了。求第一天共摘了多少。
3070个
public static void main(String[] args) {
int tao = tao(1);
System.out.println(tao);
}
public static int tao(int num) {
int day =1;
while (day<=10){
num = (num + 1) * 2;
day++;
}
return num;
}
public static void main(String[] args) {
int count = peach(1);
System.out.println(count);
}
//递归求解
public static int peach(int day) {
//第10天作为出口
if (day == 10) {
return 1;
}
//由“每天早上都吃了前一天剩下的一半零一个”,反过来则 第n天的数量 = (第n+1天的数量 + 1)* 2
return (peach(day + 1) + 1) * 2;
}