The monkey picks the Peach

Source: Internet
Author: User

Five monkeys picked a bunch of peaches together, because they were too tired, they decided to go to sleep and then divide. One of the monkeys came, it saw other monkeys did not come, then the pile of peaches evenly divided into 5 pieces, the result of one more, will eat more of this, and take away one of them. For a moment, the first 2 monkeys came, he did not know that already had a companion, thought he was the first to come, so the peach piled up on the ground, and then an average of 5, found that there is one more, the same ate the 1, and took one of them. The next 3, 4 and 5 monkeys are doing the same thing ... According to the above conditions, ask these 5 monkeys to pick at least how many peaches ? The number of Peaches left after the first 5 monkeys left ?

Enumeration method: It is very significant to find the enumeration object to improve the efficiency of enumeration, enumerate the S0, and the efficiency of enumeration S5. S0 executed 3,116 times, while S5 performed only 255 times.

#include <stdio.h>intMain () {intS0,s1,s2,s3,s4,s5,n,i;  for(s5=1;; s5++) {S4=5*s5+1; if(s4%4)            Continue; ElseS4/=4; S3=5*s4+1; if(s3%4)            Continue; ElseS3/=4; S2=5*s3+1; if(s2%4)            Continue; ElseS2/=4; S1=5*s2+1; if(s1%4)            Continue; ElseS1/=4; S0=s1*5+1;  Break; } printf ("%d%d\n", s0,s5*4); return 0;}

The monkey picks the Peach

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.