描述
有一堆桃子,猴子第一天吃了其中的一半,并再多吃了一个!以后每天猴子都吃其中的一半,然后再多吃一个。当到第10天,再想吃时(即还没吃),发现只有一个桃子了。
问题:最初共有多少个桃子
输出
第一天的桃子数
样例输出
1534
来源
代码
package draft_1;
public class D_1 {
public static void main(String[] args) {
T t = new T();
int res = t.peach(1);
if (res != -1)
System.out.println(res);
else
System.out.println("Error");
}
}
class T {
public int peach(int day) {
if (day == 10) {
return 1;
} else if (day >= 1 && day <= 9) {
return (peach(day + 1) + 1) * 2;
} else { // 判断输入是否合法
return -1;
}
}
}