The problem of the monkey's splitting peach

Source: Internet
Author: User
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}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.