Java Basics _ Monkey Peach problem __java

Source: Internet
Author: User

/*
Monkey: There's a pile of peaches on the beach, five monkeys to score.
The first monkey divided the pile of peaches by an average of five, one more,
The monkey threw one more into the sea and took a copy of it.
The second monkey divided the remaining peaches into five, and one more,
It also threw a lot of one into the sea, took a part, third, four, fifth monkey
It's all done, ask at least a lot on the beach.
One less peach.
*/

public class dividepeach{public
    static void Main (string[] args) {
        int num = 6;   The minimum number of peaches for the last peach is 6
        A: for
        (; true;num = 5) {//num++ can also, according to the number of monkeys to determine the num mantissa must be 1 or 6, so add 5 to increase the efficiency
            int temp = num;
            for (int monkey = 1; monkey <= 5; monkey++)
            {
                if (temp >= 6 && temp% 5 ==1) {  //divide, at least 5 peaches, and must be greater than Equal to 6
                    temp = TEMP/5 * 4;//The number of peaches, divided 4 times, or the total number of peaches that can be divided is now 4/5 for each monkey, and a monkey took a copy of it.
                    if (monkey = = 5) {//More than the Division method, when the 5th Monkey is satisfied to find this number of break
                        A;
                    }
                } else{//Note this else and which if is a pair of break
                    ;
                }}
        System.out.println ("The beach originally has at least" + num + "peach");
    }

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.