Kotlin algorithm for monkeys to get started

Source: Internet
Author: User
Tags reset
/* Original question monkey divided peach: There are a bunch of peaches on the beach, there are five monkeys to divide. The first monkey divided the peaches into five parts, one more, and the monkey threw one more into the sea and took a copy. The second monkey to the rest of the peach is divided into five parts, and one more, it also threw a lot of one into the sea, took a copy, the third, the fifth monkey is doing this, asked: the beach originally at least how many peaches to expand the problem of monkeys: there are a bunch of peaches on the beach, there are n monkeys to divide. The first monkey divided the pile of peaches into n parts, more K, the monkey threw a lot of one into the sea, took a copy. The second monkey to the rest of the peach is divided into n parts, and more K, it also put a lot of k thrown into the sea, took a copy, until the last monkey is also asked: the beach originally at least how many peaches first traditional problem first of all, the last remaining minimum should be 6, The lowest number of peaches is obtained by reverse recursion at the base of 6. The minimum number of peaches is added when the condition is not met. The minimum number of solutions to expand the problem is n+k, so that the minimum peach count should be returned in reverse order and the number of peaches will be added n * * When the condition is not met. PackageKotlin

classMonkeypeach {//traditional monkey divided Peachoperator FunGet (Times:int, Sum:int, lastsum:int): Int {//0 returnsreturn if(Times = = 0) {sum}else if(Times = = 5) {//Restart calculation to determine if you want to reset the dataif((sum-1)% 5 = = 0 && sum >= 6) {Get (Times-1, Sum, (sum-1)/5 * 4)}Else{Get (5, 6, 0)}}Else{//calculates if the peach is satisfiedif((lastSum-1)% 5 = = 0 && lastsum > 6) {Get (Times-1, Sum, (lastSum-1)/5 * 4)}Else Get (5, sum + 5, 0)}//custom-defined monkeys include the number of monkeys and the number of the same peaches left over each timeoperator FunGet (Monkeys:int, Losesimplenum:int): Int {ValMinnum = monkeys + losesimplenumreturnGetdiy (monkeys, minnum, 0, losesimplenum, monkeys)} FunGetdiy (Times:int, Sum:int, Lastnum:int, Losesimplenum:int, monkeys:int): Int {ValMinnum = monkeys + losesimplenumValNextmonkeys = monkeys-1/* SYSTEM.OUT.PRINTLN (times+ "sum:" +sum+ "\nlastnum:" +lastnum); *///0 returnreturn if(Times = = 0) {sum}else if(Times = = monkeys) {//Restart calculation to determine if you want to reset the dataif((sum-losesimplenum)% Monkeys = = 0 && sum >= minnum)
                {//system.out.println ("Replare"); Getdiy (Times-1, Sum, (sum-losesimplenum)/monkeys * Nextmonkeys, losesimplenum, monkeys)}Else{//system.out.println ("replare222"); Getdiy (Times, Minnum, 0, losesimplenum, monkeys)}}Else{//calculates if the peach is satisfiedif((lastnum-losesimplenum)% Monkeys = = 0 && lastnum > Minnum)
                {//system.out.println ("nest"); Getdiy (Times-1, Sum, (lastnum-losesimplenum)/monkeys * Nextmonkeys, losesimplenum, monkeys)}Else 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.