public class RecursionExercise02 {
public static void main(String[] args) {
int day = 1;
A a1 = new A();
int peachNum = a1.peach(day);
if (peachNum != -1) {
System.out.println(“第” + day + “天有” + peachNum + “个桃子”);
}
}
}
class A {
// 化繁为简思路分析逆推
// 1.day=10时,有一个桃子
// 2.day=9时,有(day10+1)*2=4
// 3.day=8时,有(day9+1)*2=10
// 规律就是:前一天的桃子=(后一天的桃子+1)*2
// 递归
public int peach(int day) {
if (day == 10) {
return 1;
} else if (day >= 1 && day <= 9) {
return (peach(day + 1) + 1) * 2;
} else {
System.out.println(“day在1-10”);
return -1;
}
}
}