猴子吃桃问题编程,初学者能看懂的编程

2025-04-11 19:14:55
推荐回答(1个)
回答1:

分析:
①设x1为前一天桃子数,设x2为第二天桃子数,则
x2=x1/2-1, x1=(x2+1)*2
x3=x2/2-1, x2=(x3+1)*2
以此类推: x前=(x后+1)*2 ;

②从第10天可以类推到第1天,是一个循环过程。程序如下:

#include
using namespace std;

int main( )
{
int day,x1,x2;
cin>>day;
x2=1;
while(day>0)
{
x1=(x2+1)*2; /*第一天的桃子数是第2天桃子数加1后的2倍*/
x2=x1;
day--;
}
cout<<"the total is "< }