-
Notifications
You must be signed in to change notification settings - Fork 0
/
2012_1_bunnies_classes.php
82 lines (55 loc) · 1.27 KB
/
2012_1_bunnies_classes.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<?php
//Define Classes
class bunny {
public $number;
public function kill(){
$this->number = round($this->number*0.75);
}
public function __construct($data){
$this->number = $data;
}
}
class newborn extends bunny {
public function growth($data){
$this->number = round($data*0.9);
}
}
class premature extends bunny {
public $preadult;
public function leave(){
$this->preadult = round($this->preadult*0.7);
}
}
fscanf(STDIN, "%d\n", $input);
//Instantiate
$adults = new bunny($input);
$newborn = new newborn(0);
$premature = new premature(0);
//read from std input
$bunnies = $adults->number;
for ($i = 15; $i<365; $i += 15) {
//Check if bunnies died
if ($bunnies == 0){
break;
}
//bunnies swapping
$premature->preadult = $premature->number;
$premature->number = $newborn->number;
//Birth
$newborn->growth($adults->number);
//mature
if ($i >= 45 ){
$premature->leave();
$adults->number += $premature->preadult;
}
//bunnies die from flu
if ( $i % 30 == 0){
$adults->kill();
$newborn->kill();
$premature->kill();
}
$bunnies = ($adults->number + $newborn->number + $premature->number);
}
echo $bunnies,"\n";
echo "Adults:",$adults->number,"\nNewborn:",$newborn->number,"\nPremature:",$premature->number,"\n\n";
?>