Recursive realization of the Java realization of Monkey's peach

Source: Internet
Author: User

5 Monkeys picked up a pile of peaches and made an appointment early the next morning. The first monkey came early, it will be the peach flat into 5 piles, more than 1, it put out of one to eat, to belong to their own a bunch of take away, will be the rest of the mix a pile, the other monkeys came exactly the same way to deal with.

Program to find out how many peaches are at least. (using recursive functions.) )


here is the recursive algorithm I wrote:

public class Peachdividealgorithm {
 private static int num;//Peaches total
 private static int count;//Monkey Total, also number
 of peaches public static int GetResult (int count) {
  if (count==0) {return
   1;
  }
  if ((num-1)%5!=0) {
   return-1;
  }
  Num= (num-1) *4/5;
  Return GetResult (count-1);
 }
 public static void Main (string[] args) {
  count=5;
  for (int i=0;i<10000;i++) {//Peach number in 10000 of the poor lift
   num=i;
   if (GetResult (count) ==1) {
    System.out.println ("The number of the Peach:" +i);}}}

Print results:

The number of the peach:3121
The number of the peach:6246
The number of the peach:9371

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.