网络编程 
首页 > 网络编程 > 浏览文章

PHP 数据结构 算法 三元组 Triplet

(编辑:jimmy 日期: 2024/10/12 浏览:3 次 )
复制代码 代码如下:
<?php
/**
* 三元组 Triplet
*
*/
class Triplet
{
private $_data = null;
// 初始化三元组
public function init($val1,$val2,$val3)
{
$this->_data[0] = $val1;
$this->_data[1] = $val2;
$this->_data[2] = $val3;
return true;
}
// 销毁三元组
public function destroy()
{
unset($this->_data);
return true;
}
// 返回第$key的值
public function get($key)
{
if($key < 1 || $key > 3) return false;
return $this->_data[$key - 1];
}
// 设置第$key元的值为$val
public function put($key,$val)
{
if($key < 1 || $key > 3) return false;
$this->_data[$key - 1] = $val;
return true;
}
// 是否按升序排序
public function isAscending()
{
return ($this->_data[0] <= $this->_data[1]) && ($this->_data[1] <= $this->_data[2]);
}
// 是否按降序排序
public function isDescending()
{
return ($this->_data[0] >= $this->_data[1]) && ($this->_data[1] >= $this->_data[2]);
}
// 获取最大值
public function max()
{
return ($this->_data[0] >= $this->_data[1])? ($this->_data[0] >= $this->_data[2])? $this->_data[0] : $this->_data[2] : ($this->_data[1] >= $this->_data[2])? $this->_data[1] : $this->_data[2];
}
// 获取最小值
public function min()
{
return ($this->_data[0] <= $this->_data[1])? ($this->_data[0] <= $this->_data[2])? $this->_data[0] : $this->_data[2] : ($this->_data[1] <= $this->_data[2])? $this->_data[1] : $this->_data[2];
}
}
//
$objTriplet = new Triplet();
echo "init:";var_dump($objTriplet->init(1,2,3)); echo "<br/>";
echo "get 1:";var_dump($objTriplet->get(1)); echo "<br/>";
echo "get 4:";var_dump($objTriplet->get(4)); echo "<br/>"; // false
echo "put 3,4:";var_dump($objTriplet->put(3,4)); echo "<br/>";
echo "max:";var_dump($objTriplet->max()); echo "<br/>";
echo "min:";var_dump($objTriplet->min()); echo "<br/>";
echo "isAscending:";var_dump($objTriplet->isAscending()); echo "<br/>";
echo "isDescending:";var_dump($objTriplet->isDescending()); echo "<br/>";
?>
上一篇:php中的三元运算符使用说明
下一篇:php中批量删除Mysql中相同前缀的数据表的代码
一句话新闻
微软与英特尔等合作伙伴联合定义“AI PC”:键盘需配有Copilot物理按键
几个月来,英特尔、微软、AMD和其它厂商都在共同推动“AI PC”的想法,朝着更多的AI功能迈进。在近日,英特尔在台北举行的开发者活动中,也宣布了关于AI PC加速计划、新的PC开发者计划和独立硬件供应商计划。
在此次发布会上,英特尔还发布了全新的全新的酷睿Ultra Meteor Lake NUC开发套件,以及联合微软等合作伙伴联合定义“AI PC”的定义标准。