题目描述
路飞买了一堆桃子不知道个数,第一天吃了一半的桃子,还不过瘾,又多吃了一个。以后他每天吃剩下的桃子的一半还多一个,到 n 天只剩下一个桃子了。路飞想知道一开始买了多少桃子。
输入
输入一个整数 n(2≤n≤30)。
输出
输出买的桃子的数量。
分析
第 n 天,剩 1个
n - 1天,一共 x / 2 - 1 = 1 ==> x = 4 个
…
代码
#include <stdio.h>
int main () {
int n;
scanf("%d", &n);
int total = 0;
for (int i = n; i >= 1; i--) {
if (i == n) {
total = 1;
} else {
total = (total + 1) * 2;
}
}
printf("%d", total);
return 0;
}