在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
针对集合这种数据结构的特点,实现如下: 1 <?php 2 class Collection{ 3 protected $_members=array(); 4 protected $_itemHasType; //标志是否指定元素的类型 5 protected $_itemType; //集合中的元素类型 6 7 public function __construct($items = [], $itemHasType = false, $itemType = NULL){ 8 $this->_itemHasType = $itemHasType; 9 $this->_itemType = $itemType; 10 $tmp = $this->getArrayableItems($items); 11 if($this->_itemHasType){ 12 foreach($tmp as $val){ 13 if($val instanceof $this->_itemType){ 14 $this->_members[] = $val; 15 } 16 } 17 }else{ 18 $this->_members = $tmp; 19 } 20 } 21 22 public function addItem($obj){ 23 if($this->_itemHasType){ 24 if(!($obj instanceof $this->_itemType)) 25 throw new Exception("The added obj is not the type of \"$this->_itemType\"!"); 26 } 27 if($this->exists($obj)){ 28 throw new Exception("Obj is already exists!"); 29 }else{ 30 $this->_members[] = $obj; 31 } 32 } 33 34 public function removeItem($obj){ 35 if(false != ($key = array_search($obj, $this->_members))){ 36 unset($this->_members[$key]); 37 }else{ 38 throw new Exception("Obj is not exists!"); 39 } 40 } 41 42 public function all(){ 43 return $this->_members; 44 } 45 46 public function length(){ 47 return sizeof($this->_members); 48 } 49 50 public function exists($obj){ 51 return in_array($obj, $this->_members); 52 } 53 54 protected function getArrayableItems($items) 55 { 56 if (is_array($items)) { 57 return array_values(array_unique($items, SORT_REGULAR)); 58 } elseif ($items instanceof self) { 59 return $items->all(); 60 } 61 return (array) $items; 62 } 63 }
该集合类包含如下属性:
该集合类包含了如下方法:
集合的应用实例: 网易笔试题:已知输入w、x、y、z,有 0<w<=p<=x, 0<y<=q<=z,且其中w、x、y、z、p、q均为正整数。将p/q加入集合中,集合中一共有多少元素。 1 <?php 2 class Collection{ 3 protected $_members=array(); 4 protected $_itemHasType; //标志是否指定元素的类型 5 protected $_itemType; //集合中的元素类型 6 7 public function __construct($items = [], $itemHasType = false, $itemType = NULL){ 8 $this->_itemHasType = $itemHasType; 9 $this->_itemType = $itemType; 10 $tmp = $this->getArrayableItems($items); 11 if($this->_itemHasType){ 12 foreach($tmp as $val){ 13 if($val instanceof $this->_itemType){ 14 $this->_members[] = $val; 15 } 16 } 17 }else{ 18 $this->_members = $tmp; 19 } 20 } 21 22 public function addItem($obj){ 23 if($this->_itemHasType){ 24 if(!($obj instanceof $this->_itemType)) 25 throw new Exception("The added obj is not the type of \"$this->_itemType\"!"); 26 } 27 if($this->exists($obj)){ 28 //throw new Exception("Obj is already exists!"); 29 }else{ 30 $this->_members[] = $obj; 31 } 32 } 33 34 public function removeItem($obj){ 35 if(false != ($key = array_search($obj, $this->_members))){ 36 unset($this->_members[$key]); 37 }else{ 38 throw new Exception("Obj is not exists!"); 39 } 40 } 41 42 public function all(){ 43 return $this->_members; 44 } 45 46 public function length(){ 47 return sizeof($this->_members); 48 } 49 50 public function exists($obj){ 51 return in_array($obj, $this->_members); 52 } 53 54 protected function getArrayableItems($items) 55 { 56 if (is_array($items)) { 57 return array_values(array_unique($items, SORT_REGULAR)); 58 } elseif ($items instanceof self) { 59 return $items->all(); 60 } 61 return (array) $items; 62 } 63 } 64 65 class Num{ 66 private $numerator; //分子 67 private $denominator; //分母 68 public function __construct($numerator,$denominator){ 69 list($this->numerator, $this->denominator) = $this->simply($numerator, $denominator); 70 } 71 private function simply($numerator,$denominator){ 72 $num = $this->gcd($numerator, $denominator); 73 return array($numerator/$num, $denominator/$num); 74 } 75 76 //获取两个数的最大公约数greatest common divisor 77 //辗转相除法。另外还可以采用更相损减法 78 private function gcd($num1,$num2){ 79 if($num1 < $num2) $this->gcd($num2, $num1); 80 while($num2 != 0){ 81 $tmp = $num1%$num2; 82 $num1 = $num2; 83 $num2 = $tmp; 84 } 85 return $num1; 86 } 87 } 88 89 function getInput(){ 90 return list($w, $x, $y, $z) = explode(' ', trim(fgets(STDIN))); 91 } 92 93 function main(){ 94 list($w, $x,$y,$z) = getInput(); 95 $w = (int)$w; 96 $x = (int)$x; 97 $y = (int)$y; 98 $z = (int)$z; 99 $numSet = new Collection([], true, 'Num'); 100 for($i=$w; $i<=$x; $i++){ 101 for($j=$y; $j<=$z; $j++){ 102 $numSet->addItem(new Num($i,$j)); 103 } 104 } 105 echo $numSet->length(); 106 } 107 main();
|
2022-08-17
2022-11-06
2022-08-17
2022-07-18
2022-07-18
请发表评论