猴子吃桃问题:猴子第一天摘下若干个桃子,当即吃了一半,还不过瘾,又多吃了一个。第二天早上又将剩下的桃子吃掉一半,又多吃一个。以后每天早上都吃了前一天剩下的一半零一个。到第10天早上想吃的时候,见只剩一个桃子了。求第一天共摘了多少桃子。
#include<stdio.h>
void main() {
int n = 1, day = 1;
printf("please input the day of spending\n");
scanf("%d", &day);
for (int i = 1; i < day; i++) {
//反向求解,数列的通项公式为n=(n+1)*2
n = (n + 1) * 2;
}
printf("The number of peaches:%d\n\n", n);
}
结果
please input the day of spending
8
The number of peaches:382