Java Classic algorithm _016 monkey eating Peach problem (recursive)

Source: Internet
Author: User
Topic: Monkey Eat Peach problem: The first day the monkey picked a number of peaches, immediately ate half, still not addicted, and eat a
The next morning he ate half the remaining peaches and ate one more.
Every morning after the last day of the rest of the half zero. On the 10th day of the morning when I wanted to eat again, I saw only one peach left.

Ask for the first day of the total number of pick.

Package wzs.arithmetics;

Question: Monkey eat peach problem: The monkey took off a number of peaches on the first day, ate half of it immediately, not addicted to it, ate one more  
///The next morning ate half of the remaining peaches, ate one more
//every morning and ate half the rest   of the day before. On the 10th day of the morning when I wanted to eat again, I saw only one peach left.
//Ask for a total number of 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);
    }

    /**
     * Number of peaches per day *
     @param days
     * @return The number of Peaches
     /
    static int total (int days)
    {
        If day = =)
        {return
            1;
        }
        else
        {return
            (Total (day + 1) + 1) * 2;
        }
}}

Output results:

1534
766
382
190
4
1



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.