- 题目:
- 猴子吃桃问题:猴子第一天摘下若干个桃子,当即吃了一半,还不瘾,
- 又多吃了一个 第二天早上又将剩下的桃子吃掉一半,又多吃了一个。
- 以后每天早上都吃了前一天剩下 的一半零一个。
- 到第10天早上想再吃时,见只剩下一个桃子了。求第一天共摘了多少。
public class Demo02 {
public int getNumber(){
int total = 1;
//day = 1的时候,算出来的total其实是第9天有的桃子
//day = 9的时候,算出来的total就是第1天的桃子
for(int day = 1; day < 10; day++){
total = 2 * (total + 1);
}
return total;
}
public static void main(String[] args) {
System.out.println("一开始共有 " + new Demo02().getNumber() + " 桃子");
}
}