猴子吃桃问题
猴子吃桃问题。猴子第1天摘下若干个桃子,当即吃了一半,还不过瘾,又多吃了一个。第2天又将剩下的桃子吃了一半,又多吃了一个。以后每天都吃了前一天剩下的一半另加一个。到第10天早上想再吃时,只剩一个桃子了。求第一天共摘了多少桃子。
程序代码:
①#include <stdio.h>
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char *argv[]) {
int day,x1,x2;
day=9;
x2=1;
while(day>0){
x1=(x2+1)*2;
x2=x1;
day--;//应为从后向前推,所以天数递减
}
printf("the total is %d\n",x1);
return 0;
}
②扩展验证
#include <stdio.h>
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char *argv[]) {
int day,x1,x2;
day=1;
x2=1534;
while(day>10)
{
x1=(x2/2)-1;
x2=x1;
day++;
}
printf("the number on day 10 is %d\n ",x1);
return 0;
}