The recursive algorithm and non-recursive algorithm of the cow's offspring.

Source: Internet
Author: User


A cow, a cow and a bull at the end of the second, a cow at the end of the year, and a cow at the beginning of the third. Bulls can only live for four years. A farm began with only a newborn cow, n years after the total number of cattle. Please write a function output, recursive and non-recursive two methods to achieve. function Cowrecursion ($i) {if ($i = = 1)//If it is the first year, then 1 cows. {return 1;} ElseIf ($i = = 2) {return 2 + cowrecursion (1);//First Cow and Sons + second cow first year}elseif ($i = = 3) {return 2 + cowrecursion (2) + Cowrecursio n (1);  First Cow and Sons + Second cow second year + third cow first year}elseif ($i ==4) {return 2 + cowrecursion (3) + cowrecursion (2);    First Cow and Sons + Second cow third year + third cow second year}//elseif ($i = = 5)//{//Return Cowrecursion (4) + cowrecursion (3); The first cow died. The bull is dead, too.  Second cow, year four + third cow third year//}elseif ($i >= 5) {return cowrecursion ($i-1) + cowrecursion ($i-2); }}//non-recursive implementation of function Cow_norecursion ($i) {///implement ideas, stored in an array, value of values represents the age.  Loop plus 1. $cows = Array (1); In the first year, 1 cows.  $cowsmale = Array (); Used to store bull for ($j =0; $j < $i; $j + +)//cycle for years {//Cycle cows $cows _copy = $cows; foreach ($cows as $key + = $value) {switch ($cows _cop    y[$key]) {case 1:break;case 2: $cows _copy[] = 1;    $cowsmale [] = 1;    Break;case 3: $cows _copy[] = 1; Break;case 4:break;case 5:unset($cows _copy[$key]); Break;}} $cows = $cows _copy;array_walk ($cows, function (& $value, $index) {if ($value > 0) $value + +;});    $cowsmale _copy = $cowsmale; Cyclic bull foreach ($cowsmale as $d + $value) {$cowsmale _copy[$d]++;if ($cowsmale _copy[$d] = = 5)//By the end of the year {unset ($cow    smale_copy[$d]); }} $cowsmale = $cowsmale _copy; }return count ($cows) + count ($cowsmale); }echo "&LT;BR/>list totol--->". Cow_norecursion (+), echo "&LT;BR/>totol recursion--->". Cowrecursion (26) ;





End

The recursive algorithm and non-recursive algorithm of the cow's offspring.

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.