<?php
/*
* There is a cow, to four years old can bear, each year, the birth is the same cow, 15-year-old sterilization, 20-year-old death
* How many cows are there after n years?
* The subject needs to be calculated using Recursive functions
* Condition 1: When the cattle are 4-15 years old, they can be born
* Condition 2: When the ox dies at the age of 20
*/
$n for years
function sum ($n)
{
Define a count result
static $num = 1;
Start the cycle to determine the age of the cow
for ($i =1; $i <= $n; $i + +)
{
if ($i >=4&& $i <15)
{
Fertile year can be born
$num + +;
Judging how many children can be born this year, to meet the conditions, the same plus, no
SUM ($n-$i);
}
When he was 20 years old, he died.
if ($i = = 20)
{
$num--;
}
}
return $num;
}
echo sum (10);
Summary: The subject of a bit dizzy, in fact, is not difficult, after must be a lot of practice. With recursive functions, the number of cows is calculated, two conditions, recursive
This year, a few can be born, each call is a cow's age, when reaching 4 years old, fertile, 15 years old, not born, 20-year-old death.