Five monkeys in peach, algorithm & interview __ algorithm

Source: Internet
Author: User
1. Five monkeys divide the peach. In the middle of the night, the first monkey got up first, it divided the peach into five equal piles, one more. So, it ate one, took a bunch; the second monkey got up and looked, only four piles of peaches. So the four piles together, divided into equal five piles, and one more. So, it also ate one, took a bunch of ... A few other monkeys are also divided. Q: How many of these peaches are at least.
Reference answer: First give this pile of Peaches 4, set at this time a total of x peach, finally left a peach. this:
After the first monkey divided after the left: (1-1/5) x= (4/5) X;
The second monkey divided after the left: (1-1/5) 2X;
After the third monkey divided after the left: (1-1/5) 3X;
The fourth monkey divided after the left: (1-1/5) 4X;
The fifth monkey was left after it was divided: (1-1/5) 5x= (1024/3125) X;
To: A= (1024/3125) X;
To make a an integer, X takes a minimum of 3125.
Minus the plus 4, so there are at least 3,121 of these peaches.


Java programming implementations:

public class Tao {public
	static void Main (string[] args) {
		int monkey=1;//number of monkeys to divide peach number
		int peaches=1;//Peach
		int peach=1;//The total number of points per peach
		//cycle to give each monkey a peach while
		(monkey<=5)
		{
			//If the success of the Peach if
			(peach%5==1&& peach/5!=0)
			{
				//The total number of peach can be divided into the present 4/5
				peach= (PEACH/5);
				Replace a monkey
				monkey++;
			}
			If it fails, the peach can be divided by the number of Peaches added 1
			else
			{
				peaches++;
				peach=peaches;
				Monkey=1;
			}
		}
		System.out.println ("The minimum number of Peaches is:" +peaches);}
}
Programming section for original, algorithmic reference link: http://blog.csdn.net/v_july_v/article/details/6803368

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.