Problem Description:
Five monkeys are divided into peaches. In the middle of the night, the first monkey get up first, it divides the peach into equal five piles, one more. So it ate one and took a bunch; the second monkey got up and looked, only four peaches. So the four piles together, divided into equal five piles, and one more. So, it also ate one, took a bunch of; Some of the other monkeys are divided like this. Q: There are at least as many peaches in this pile. Thinking Analysis:
Set the total number of Peaches for S, the first monkey points and peaches for X5, the second monkey for X4 ... The fifth monkey is X1.
S = 5 * x5 + 1;
4 * x5 = 5 * x4 + 1;
......
4 * x2 = 5 * x1 + 1;
So there are:
4 (x5 + 1) = 5 (x4 + 1), 4 (x4 + 1) = 5 (x3 + 1) ..., 4 (x2+1) = 5 (x1 + 1)
So there are: (x5 + 1) = (5/4) ^4 * (x1 + 1)--> (x1 + 1) = (4/5) ^4 * (x5 + 1)
Because x1 must be an integer, so x5 = 5^4-1 = 624
S = 5 * x5 + 1 = 624 * 5 + 1 = 3121 code Example:1 #include <stdio.h>
2 #include <stdlib.h>
3
4intMonkey_peach (intN
5 {
6int*p;
7intI
8
9 p = (int*) malloc (n *sizeof(int));
10if(p = = NULL)
11 return-1;
12
P[0] = 1;
14
15 while(1)
16 {
17 for(i = 1; i < n; i++)
18 {
19if((p[i-1] * 5 + 1)% 4!= 0)
20 Break;
P[i] = (p[i-1] * 5 + 1)/4;
22}
23
24if(I >= N)
25 Break;
p[0]++;
27}
28
29 for(i = 0; i < n; i++)
30 {
To printf ("%d", p[i]);
32}
printf ("\ n");
printf ("Peach count =%d.\n", 5 * p[n-1] + 1);
35 return0;
36}
37
38intMain ()
39 {
Monkey_peach (5);
41 return0;
42}