Support original article, reprint please indicate the source: http://blog.csdn.net/friendan/article/details/8829530
--------------------------------------------------------------------------------------------------------------- -------------------
have such a problem;
Monkey Eating Peach problem: The first day the monkey picked a number of peaches, immediately ate half, still not enjoyable, and eat a more,
The next morning he ate half the remaining peaches and ate one more. Every morning after the last day of the rest of the half zero.
On the 10th day of the morning when I wanted to eat again, I saw only one peach left. Ask for a total number of peaches on the first day.
The answer is: 1534.
--------------------------------------------------------------------------------------------------------------- ----------------
Topic Analysis:
The number of peaches on the first day equals the number of peaches in the second day plus 1 and multiplied by 2.
The number of peaches on the second day equals the number of peaches on the third day plus 1 and multiplying by 2.
The number of peaches on the third day equals the number of peaches on the fourth day plus 1 and multiplying by 2.
The number of peaches on the fourth day equals the number of peaches on the fifth day plus 1 and multiplying by 2.
The number of Peaches on the fifth day equals the number of peaches on the sixth day plus 1 and multiplying by 2.
The number of peaches on the sixth day equals the number of peaches on the seventh day plus 1 and multiplying by 2.
The number of peaches on the seventh day equals the number of peaches on the eighth day plus 1 and multiplying by 2.
The number of peaches on the eighth day equals the number of peaches on the Nineth day plus 1 and multiplying by 2.
The number of peaches on the nineth day equals the number of peaches in the day plus 1 and multiplied by 2.
.../haha, the number of peaches on the day is known to be 1, now you can reverse the number of peaches on the first day.
Obviously, the number of peaches the day before is equal to the number of days after that plus 1 and multiplied by 2.
--------------------------------------------------------------------------------------------------------------- ----------
Through the above analysis, we find that the number of peaches per day is similar to the process, so we can pass
Return to solve the problem. The output of a recursive function is n equals 10, and the parameter is N, which represents the nth day.
--------------------------------------------------------------------------------------------------------------- -------
The code example I wrote is as follows:
#include <iostream>
using namespace std;
Find the number of peaches for the first day
int Peach (int n)
{
int sum=0;
if (10==n)
{return
1;
}
else
{
Sum=peach (n+1); The number of peaches in the next day
sum= (sum+1) *2;//N Days Peach number
}
if (1==n)
{
printf ("How many%d peaches are picked on the first day \ n", sum);
}
Return sum;//returns the number of N-day Peaches
}
void Main (void)
{
Peach (1);
Cin.get ();
}
--------------------------------------------------------------------------------------------------------------- ----------------------------------------------------------------------------------
Screenshot of Effect
--------------------------------------------------------------------------------------------------------------- ------------------------------------------------------------------------------------
your very satisfaction is my pursuit of the purpose.
A little advice from you is my motivation to follow.