This article is an example of the realization of C language monkey eating peach problem. Share to everyone for your reference, specific as follows:
Problem:
The first day the monkey picked n a peach, then ate half, still not enjoyable, and then ate one. The next day he ate half the remaining peaches and ate one more. Every day after eating the last half of the remaining one. On the 10th day when you want to eat, there is only one peach left, ask the first day a total number of peaches off?
Analytical:
① from the last day of the x=1, pour out the number of X days before, you need to note that the expression is x=2 (x+1), rather than x=2x+1, pay attention to the difference between the two, think clearly why the second is not correct.
② the expression as the loop body that loops 9 times and sets a breakpoint at the statement to observe.
Specific procedures:
#include <stdio.h>
int main ()
{
int i;
int x=1;
for (i=0;i<9;i++)
x = 2* (x+1);
printf ("%d\n", x);
return 0;
}
The result of program operation is: 1534.
I hope this article will help you with the C language program design.