php编写的小demo(斐波那契数列、直角三角形、冒泡、二分、顺序)

单例模式:只允许有一个对象,不允许克隆,初始化,
好处:类似db类操作数据库,当前只允许一个对象调用,但是看资料好像php又不能实现,底层原理基础太差。new 一个对象是一个非常消耗资源的方式,所以我们可以使用静态方法初始化一个对象。
使用了强制类型,int、array

<?php 
// 单例模式
class Danli
{
	// 私有化的静态保存属性
	private static $obj = null;

	// 私有化构造方法
	private function __construct(){}

	// 私有化克隆方法
	private function __clone(){}

	// 公共的静态调用方法
	public static function getInta()
	{
		// instanceof 表示访问的对象属不属于当前对象。
		if(!self::$obj instanceof self)
		{
			self::$obj = new self();
		}

		return self::$obj;
	}

	// 公共直角三角形
	public function RigthAngle(int $i)
	{
		for($a = 0;$a <= $i;$a++)
		{
			for($b = 0; $b <= $a; $b++)
			{
				echo '*'.'&nbsp';
			}
			echo '<br />';
		}
	}

	# 公共倒立三角形
	public function HandsRight(int $i)
	{
		for($a = 0;$a <= $i; $a++)
		{
			for($b = 10; $b >= $a; $b--)
			{
				echo '*'.'&nbsp';
			}
			echo '<br />';
		}
	}

	// 等腰三角形
	public function Isosceles(int $i)
	{
		for($a = 0; $a <= $i; $a++)
		{
			for($b = 10; $b >= $a; $b--)
			{
				echo '&nbsp';
			}
			for($c = 0; $c <= $b; $c++)
 			{
 				echo '*'.'&nbsp';
 			}
			echo '<br />';
		}
	}

	// 倒立等腰三角形
	public function HandsIso(int $i)
	{
		for($a =0; $a <= $i; $a++)
		{
			for($b = 10; $b > $a; $b--)
			{
				echo '&nbsp;'.'*';
			}
			echo '<br />';
			for($c = 0; $c <= $b; $c++)
			{
				echo '&nbsp';
			}

		}
	}

	// 99乘法表
	public function NineTable(int $i)
	{
		for($a = 1; $a <= $i; $a++)
		{
			for($b = 1; $b <= $a; $b++)
			{
				// 正常情况可以嵌套在html下 使用tr  td 标签
				echo $a .'*'.$b .'=' . $a * $b .'<br/>';
			}
		}
	}

	# 递归实现斐波那契数列
	public function Sequence(int $i)
	{
		if($i == 1 || $i == 2)
		{
			return 1;
		}

		return $this->Sequence($i - 1) + $this->Sequence($i - 2);
	}

	# 递推实现斐波那契数列
	public function Recursive(int $i)
	{
		if($i == 1 || $i == 2)
		{
			return 1;
		}

		if($i == 3)
		{
			return 2;
		}

		$first = 1;
		$second = 2;

		for($a = 3; $a < $i; $a++)
		{
			$t = $first + $second;
			$first = $second;
			$second = $t;
		}

		return  $t;
	}

	// 随机双色球
	public function Dobule(int $i,int $j,int $bule)
	{
		# 抽取1-31的随机数
		$red_arr = range($i, $j);

		# 随机抽取6位数字
		$red_res = array_rand($red_arr, 6);

		# 打乱数组健值
		shuffle($red_res);

		foreach($red_res as $v)
		{
			# 如果当前是数字小于10,则在数字前面加0
			$red[] = $red_arr[$v] < 10 ? ('0'.$red_arr[$v]) : $red_arr[$v];
 		}

 		$blue_arr = range($i,$bule);
		
		$blue_res = array_rand($blue_arr,1);

		$blue = $blue_res < 10 ? ('0'.$blue_res) : $blue_res;

		foreach($red as $v)
		{
			echo $v.'&nbsp';
		} 
		echo $blue;
	}

	// 冒泡排序
	public function Buulle(array $arr)
	{
		$len = count($arr)-1;

		for($i = 0; $i < $len;$i++)
		{
			for($j = 0; $j < $len - $i; $j++)
			{
				if($arr[$j] > $arr[$j+1])
				{
					$temp = $arr[$j];
					$arr[$j] = $arr[$j+1];
					$arr[$j+1] = $temp;
				}
			}
		}
		return $arr;
	}

	// 二分查找法 数组必须要是一个有序数组
	public function Sereach(int $res)
	{
		$arr = [12,25,36,47,57,65];

		$len = count($arr)-1;

		$count = 0;

		$mid = floor($len / 2);

		while($count <= $len)
		{
			if($res < $arr[$mid])
			{
				$len = $mid - 1;

			}else if($res > $arr[$mid])
			{
				$count = $mid + 1;
			}else{
				# 注意当前找到的是下标
				return $mid;
			}

			$mid = floor(($count + $len) / 2);
		}
		return "没有找到";
	}

	// 顺序查找法
	public function Order(int $res)
	{
		$arr = [12,25,36,47,57,65];
		for($i = 0;$i < count($arr); $i++)
		{
			if($res == $arr[$i])
			{
				return $i;
			}

		}
		return "没有找到";
	}
}

# 创建对象
$Danli = Danli::getInta();

# 顺序查找法
# $res = 122;
# echo $Danli->Order($res);

# 二分查找法
# $res = 12;
# echo $Danli->Sereach($res);


# 冒泡排序
# $arr = [13,32,4,12,51,22];
# echo '<pre>';
# print_r($Danli->Buulle($arr));

# 直角三角形
# $Danli->RigthAngle(10);

# 倒立直角三角形
$Danli->HandsRight(10);

# 等腰三角形
# $Danli->Isosceles(10);

# 倒立等腰三角形
# $Danli->HandsIso(10);

# 99乘法表
# $Danli->NineTable(9);

# 递归实现斐波那契数列
# echo $Danli->Sequence(15);

# 递推实现菲波那切数列
# echo $Danli->Recursive(15);
 
# 随机双色球
# $Danli->Dobule(1,31,16);
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值