Java classic algorithm _ 030 monkey peach (recursive solution)

Source: Internet
Author: User
Package com. arithmetic; // question: there are a bunch of peaches on the beach, and five monkeys can score points. The first monkey divided the pile of peach creden into five portions, one more. The monkey threw the other one into the sea and took one. The second monkey divides the remaining peaches into five equal portions and adds one more portion. It also throws one more portion into the sea and takes one portion, // The third, fourth, and fifth monkeys do this. How many peaches are there on the beach? // Analysis: If you want to have at least one peach, the fifth monkey will take at least one peach, and there will be six peaches when dividing the peach. public class test_wzs30 {public static void main (string [] ARGs) {for (INT I = 5; I> 0; I --) {system. out. println ("no." + I + "monkeys have peaches:" + dividepeaches (I ));}} /*** peach ** @ Param Number * @ return */static int dividepeaches (INT number) {If (number = 5) {return 6 ;} else {return dividepeaches (number + 1) * 5 + 1 ;}}}

Output result:

5th monkeys have peaches: 4th monkeys have peaches: 31 3rd monkeys have peaches: 156 2nd monkeys have peaches: 781 1st monkeys have peaches: 3906 monkeys have peaches:

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.