本篇文章是对PHP冒泡、PHP二分法、PHP求素数、PHP乘法表进行了详细的分析介绍,需要的朋友参考下
PHP几个算法整理 涉及到以下几个示例。
PHP冒泡
PHP二分法
PHP求素数
PHP乘法表
PHP冒泡法 示例
复制代码 代码如下:
//PHP冒泡 从小到大
function maopao(&$arr)
{
if(!empty($arr))
{
for($i=0;$i
if($arr[$i]>$arr[$j])
{
//开始交换
$temp = $arr[$i];
$arr[$i] = $arr[$j];
$arr[$j] = $temp;
}
}
}
return $arr;
}
}
php二分法查找 代码示例
复制代码 代码如下:
//二分法查找
function erfenfa($a,$arr)
{
print_r($arr);
if(!empty($a) && !empty($arr))
{
$start = 0;
$end = count($arr)-1;
$i = 0;
while($start
$i ++;
$step = floor($end / 2);
if($a == $arr[$step])
{
print_r($arr[$step]);
return $a;
}
if($a >$arr[$step])
{
$start = $step;
}
if($a
{
$end = $step;
}
}
}
}
php求素数 – 计算 a 到 b 之间的素数。 代码示例
复制代码 代码如下:
//php求素数 - 计算 a 到 b 之间的素数。
function sushu($a,$b)
{
if(!empty($a) && !empty($b))
{
if($b $temp = array();
for($i=$a;$i {
$j = intval(sqrt($i));
$flag = true;
if($i
{
$temp[$i] = $i;
}else
{
for($x=2;$x
{
if($i%$x==0)
{
$flag = false;
break;
}
}
if($flag)
{
$temp[$i] = $i;
}
}
}
return $temp;
}
}
PHP输出乘法表-递归 代码示例
复制代码 代码如下:
//PHP输出乘法表-递归
function digui($a,$step)
{
if($a >$step) return;
if( !empty($a) && !empty($step) )
{
for($i=1;$i {
echo $i.'*'.$a.'='.$a*$i.”\t”;
if($i == $a ) echo ‘
‘;
}
$a = $a + 1;
digui($a,$step);
}
}
PHP输出乘法表-循环 代码示例
复制代码 代码如下:
//PHP输出乘法表-循环
function chengfa($a,$step)
{
if( !empty($a) && !empty($step) )
{
for($i=$a;$i {
for($j=1;$j
{
echo $j.'*'.$i.'='.$i*$j.”\t”;
if($i==$j) echo ‘
‘;
}
}
}
}
,虚拟主机,服务器空间,香港空间

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Notepad++7.3.1
Easy-to-use and free code editor
