Java solution to the Monkey Peach Algorithm solution and expand the monkey algorithm to customize the solution

Source: Internet
Author: User
Tags static class

/* Original Problem Monkey: There is a pile of peaches on the beach, five monkeys to score.
The first monkey put the pile of peaches into five, one more, the monkey threw a lot of one into the sea, took a copy.
The second monkey divided the remaining peaches into five, and one more, it also threw more than one into the sea, took a copy,
Third, four, fifth monkeys do this,
Ask:
At least a few peaches on the beach.

Expand the problem monkey: there is a pile of peaches on the beach, there are n monkeys to divide.
The first monkey divided the pile of peaches into N, more k, and this monkey threw a lot of one into the sea, took a copy.
The second monkey to the rest of the peach and flat into N, and more K, it also put more k into the sea, took a copy,
That's what the last monkey did.
Ask:
At least a few peaches on the beach. The first traditional problem is that the minimum should be 6, with 6 as the beginning of the base in reverse order to find the minimum number of peaches when not meet the conditions of the peach number plus 5 to expand the problem of the smallest solution left should be n+k, In order to find the minimum number of peaches when the condition is not satisfied, make the peach count plus n directly.

*/

public static Class get{
The traditional monkey is divided into peach
public static int Get (int times, int sum, int lastsum) {
0 is returned
if (times = = 0) {
return sum;
}else if (times = = 5) {
Start the calculation again to determine if you want to reset the data
if ((sum-1)%5==0&&sum>=6) {
Return to get (Times-1, Sum, (sum-1)/5*4);
}else {
Return to get (5, 6, 0);
}
}else {
Calculate whether to satisfy this one time peach
if ((lastSum-1)%5==0&&lastsum>6) {
Return to get (Times-1, Sum, (lastSum-1)/5*4);
}else return get (5,sum+5,0);
}
}
Custom monkeys include the number of monkeys and the number of peaches left behind each time.
public static int get (int monkeys,int losesimplenum) {
int minnum = Monkeys+losesimplenum;
Return Getdiy (monkeys, minnum, 0, losesimplenum, monkeys);
}

public static int Getdiy (int times, int sum, int lastnum,int losesimplenum,int monkeys) {
int minnum = Monkeys+losesimplenum;
int nextmonkeys = monkeys-1;
/* SYSTEM.OUT.PRINTLN (times+ "sum:" +sum+ "\nlastnum:" +lastnum);
0 is returned
if (times = = 0) {
return sum;
}else if (times = = Monkeys) {
Start the calculation again to determine if you want to reset the data
if ((sum-losesimplenum)%monkeys==0&&sum>=minnum) {
System.out.println ("Replare");
Return Getdiy (Times-1, Sum, (sum-losesimplenum)/monkeys*nextmonkeys,losesimplenum,monkeys);
}else {
System.out.println ("replare222");
Return Getdiy (Times, Minnum, 0,losesimplenum,monkeys);
}
}else {
Calculate whether to satisfy this one time peach
if ((lastnum-losesimplenum)%monkeys==0&&lastnum>minnum) {
System.out.println ("nest");
Return Getdiy (Times-1, Sum, (lastnum-losesimplenum)/monkeys*nextmonkeys,losesimplenum,monkeys);
}else return Getdiy (monkeys, Sum+monkeys, 0,losesimplenum,monkeys);
}
}
}

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.