There are also cute monkey you can not think of - classic examples: Monkey eating peaches

I heard that you like to eat monkey peaches time. Do you know how monkeys eat a peach should describe it?

Of course, in the programming world, this would be very interesting!

Today to share with you is that classic example - a monkey eating peaches problem, with a look ~

Classic example:

Monkey off his first day of a number of peach, half eaten immediately, not addiction, but also eat a
morning in turn eaten by the remaining peach half, then eat one. After the morning before the rest of the day eat
half a zero. When the morning of the 10th day want to eat, see only one peach. Seeking first day were picked number.

A brief analysis: see here, you would think of using loops and recursion can be solved. Of course, you need to think about the problem of reverse thinking, if thinking forward from the front can solve it? The following is a recursive loop and the reverse solved.

#include <stdio.h>
#include <stdlib.h>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int pear();

int n = 1;

int main(int argc, char *argv[]) {
	int x, i, m=1;
	
	for (i = 0; i < 9; i++){
		m = pear();	
		printf("%d ",m);	
	}
	printf("%d",m);
	
	return 0;
}

int pear(){
	n = 2 * (n + 1);
	
	return n;
}

At the end of this blog would like to ask a question, there are other ways of thinking to solve it?

Guess you like

Origin blog.csdn.net/weixin_44566432/article/details/88031681