一隻猴子摘了一堆桃子,每天都吃掉剩下的一半,再多吃 1 個。到第 days 天早上,只剩 1 個了。寫 peaches(days):第 1 天一共有幾個桃子?比如 days 是 2:第 2 天剩 1 個,第 1 天就是 (1 + 1) × 2 = 4 個。days 是 1 時就是 1 個。
peaches(10)→1534peaches(2)→4peaches(3)→10int peaches(int days)int peaches(int days) {
int x = 1;
for (int d = days - 1; d >= 1; d--) {
// 在這裡寫:從後一天的個數,算出前一天的個數
}
return x;
}
寫好了先點「執行」試試示例,沒問題再點「提交」。