Java classic algorithm _ 016 monkey peach eating problem (recursion)

Source: Internet
Author: User

// Question: The Monkey eats peach. The monkey picks up several peaches on the first day and eats half of them immediately.
// In the morning of the next day, I ate half of the remaining peaches and one more peach.
// In the future, I eat the remaining half of the previous day every morning. When you want to eat again in the morning of the seventh day, there is only one peach left.

// Calculate the total number on the first day.

Package WZS. arithmetics; // question: a monkey eats peaches. The monkey picks up a few peaches on the first day and eats half of them instantly, I have eaten another one. // I have eaten the remaining peaches in half the next morning, and I have eaten another one. // then I have eaten the remaining half of the previous day every morning. When you want to eat again in the morning of the seventh day, there is only one peach left. // Calculate the total number on the first day. Public class test_wzs16 {public static void main (string [] ARGs) {system. out. println (Total (1); system. out. println (total (2); system. out. println (total (3); system. out. println (total (4); system. out. println (total (5); system. out. println (total (6); system. out. println (total (7); system. out. println (total (8); system. out. println (total (9); system. out. println (total (10);}/*** Number of peaches per day * @ Param day * @ return number of peaches per day */static int total (INT Day) {If (Day = 10) {return 1;} else {return (total (day + 1) + 1) * 2 ;}}}

Output result:

15347663821909446221041

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.