Skip to content

Commit 1baacbf

Browse files
authored
Merge pull request #41 from openset/master
Update: GetCattle 算法
2 parents c4f0571 + 274c907 commit 1baacbf

File tree

1 file changed

+8
-9
lines changed

1 file changed

+8
-9
lines changed

package/Other/GetCattle.php

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,18 @@
2121
function getCattle($n)
2222
{
2323
static $num = 1;
24-
for ($i =1; $i<=$n;$i++){
25-
if( $i == 20) break;
26-
if($i >= 4 && $i <15){
27-
if($i % 4 == 0 ){
28-
getCattle($n - $i);
29-
$num++;
30-
}
31-
$num++;
24+
for ($i = 1; $i <= $n; $i++) {
25+
if ($i == 20) {
26+
$num--; //死亡需减一
27+
} else if ($i >= 4 && $i < 15) {
28+
$num++; //生小母牛(这里有小母牛)
29+
getCattle($n - $i); //小母牛生小母牛(这里不包含小母牛)
3230
}
3331
}
3432
return $num;
3533
}
36-
echo '牛年共有:'.getCattle(10);
34+
35+
echo '牛年共有:' . getCattle(10);
3736

3837
/*
3938
123456789

0 commit comments

Comments
 (0)