The classical algorithm of C language--the problem of monkey stealing peach

Source: Internet
Author: User

Title: There are a bunch of peaches on the beach, five monkeys to divide. The first monkey divided this pile of peaches into five copies, one more, and the monkey threw one more into the sea and took a copy. The second monkey to the rest of the peach evenly divided into five parts, and one more, it also threw a lot of one into the sea, took a copy, the third, the fifth monkey is doing this, ask the beach at least how many peaches?

Algorithm Analysis://initial peach number is sum
First time: sum=5*x1+1
Second time: 4*x1=5*x2+1
Third time: 4*x2=5*x3+1
Fourth time: 4*x3=5*x4+1
Fifth time: 4*x4=5*x5+1
Fifth allocation end: The rest 4*x5 a peach, and x5>=1, so i=4*x5>=4. Push j= (I/4) *5+1;i=j each time, after each push end to determine whether j%4 is equal to 0, if 0 continue to push, otherwise update I, until found to be able to complete five push the integer i, finally can get the original number of peaches.

C Language Program:

#include <stdio.h>intMainintAGRC,Char*agrv[]) {    intI, M, J, K, Count;  for(i =4; i<10000; i + =4)/*I assign 5 monkeys to the last remaining peach after the peach, must be a multiple of 4, and then based on this, push up 5 times, if it can be pushed to the fifth time, then the remaining Peach number I is satisfied with the condition, otherwise, the number of remaining peaches to find the minimum number of remaining peaches to meet the conditions*/{Count=0; M=i;  for(k =0; k<5; k++) {J= I/4*5+1; I=J; if(J%4==0) Count++; Else                 Break; } I=m; if(Count = =4) {printf ("the original minimum number of Peaches is:%d\n", J);  Break; }} system ("Pause"); return 0;}

The classical algorithm of C language--the problem of monkey stealing peach

Related Article

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.