来源:转载 发布时间:2018-07-03 14:31:33 阅读量:838
PHP几个算法整理-PHP冒泡-PHP二分法-PHP求素数-PHP乘法表
PHP几个算法整理 涉及到以下几个示例。
PHP冒泡
PHP二分法
PHP求素数
PHP乘法表
PHP冒泡法 示例
view plaincopy to clipboardprint?
1. //PHP冒泡 从小到大
2. function maopao(&$arr)
3. {
4. if(!emptyempty($arr))
5. {
6. for($i=0;$i<count($arr);$i++)
7. {
8. if($arr[$i]>$arr[$j])
9. {
10. //开始交换
11. $temp = $arr[$i];
12. $arr[$i] = $arr[$j];
13. $arr[$j] = $temp;
14. }
15. }
16. }
17. return $arr;
18. }
19. }
//PHP冒泡 从小到大
function maopao(&$arr)
{
if(!empty($arr))
{
for($i=0;$i<count($arr);$i++)
{
if($arr[$i]>$arr[$j])
{
//开始交换
$temp = $arr[$i];
$arr[$i] = $arr[$j];
$arr[$j] = $temp;
}
}
}
return $arr;
}
}
php二分法查找 代码示例
view plaincopy to clipboardprint?
1. //<span class="wp_keywordlink"><a href="http://www.gosoa.com.cn/ " title="php开发">php</a></span>二分法查找
2. function erfenfa($a,$arr)
3. {
4. print_r($arr);
5. if(!emptyempty($a) && !emptyempty($arr))
6. {
7. $start = 0;
8. $end = count($arr)-1;
9. $i = 0;
10. while($start <= $end) {
11. $i ++;
12. $step = floor($end / 2);
13. if($a == $arr[$step])
14. {
15. print_r($arr[$step]);
16. return $a;
17. }
18. if($a > $arr[$step])
19. {
20. $start = $step;
21. }
22.
23. if($a < $arr[$step])
24. {
25. $end = $step;
26. }
27. }
28. }
29. }
//php二分法查找
function erfenfa($a,$arr)
{
print_r($arr);
if(!empty($a) && !empty($arr))
{
$start = 0;
$end = count($arr)-1;
$i = 0;
while($start <= $end) {
$i ++;
$step = floor($end / 2);
if($a == $arr[$step])
{
print_r($arr[$step]);
return $a;
}
if($a > $arr[$step])
{
$start = $step;
}
if($a < $arr[$step])
{
$end = $step;
}
}
}
}
php求素数 – 计算 a 到 b 之间的素数。 代码示例
view plaincopy to clipboardprint?
1. //php求素数 - 计算 a 到 b 之间的素数。
2. function sushu($a,$b)
3. {
4. if(!emptyempty($a) && !emptyempty($b))
5. {
6. if($b<$a) return;
7. $temp = array();
8.
9. for($i=$a;$i<=$b;$i++)
10. {
11. $j = intval(sqrt($i));
12. $flag = true;
13. if($i<=3)
14. {
15. $temp[$i] = $i;
16. }else
17. {
18. for($x=2;$x<=$j;$x++)
19. {
20. if($i%$x==0)
21. {
22. $flag = false;
23. break;
24. }
25. }
26. if($flag)
27. {
28. $temp[$i] = $i;
29. }
30. }
31. }
32. return $temp;
33. }
34. }
//php求素数 - 计算 a 到 b 之间的素数。
function sushu($a,$b)
{
if(!empty($a) && !empty($b))
{
if($b<$a) return;
$temp = array();
for($i=$a;$i<=$b;$i++)
{
$j = intval(sqrt($i));
$flag = true;
if($i<=3)
{
$temp[$i] = $i;
}else
{
for($x=2;$x<=$j;$x++)
{
if($i%$x==0)
{
$flag = false;
break;
}
}
if($flag)
{
$temp[$i] = $i;
}
}
}
return $temp;
}
}
PHP输出乘法表-递归 代码示例
view plaincopy to clipboardprint?
1. //PHP输出乘法表-递归
2. function digui($a,$step)
3. {
4. if($a > $step) return;
5. if( !emptyempty($a) && !emptyempty($step) )
6. {
7. for($i=1;$i<=$a;$i++)
8. {
9. echo $i.'*'.$a.'='.$a*$i."/t";
10. if($i == $a ) echo '
11. ';
12. }
13. $a = $a + 1;
14. digui($a,$step);
15. }
16. }
//PHP输出乘法表-递归
function digui($a,$step)
{
if($a > $step) return;
if( !empty($a) && !empty($step) )
{
for($i=1;$i<=$a;$i++)
{
echo $i.'*'.$a.'='.$a*$i."/t";
if($i == $a ) echo '
';
}
$a = $a + 1;
digui($a,$step);
}
}
PHP输出乘法表-循环 代码示例
view plaincopy to clipboardprint?
1. //PHP输出乘法表-循环
2. function chengfa($a,$step)
3. {
4. if( !emptyempty($a) && !emptyempty($step) )
5. {
6. for($i=$a;$i<=$step;$i++)
7. {
8. for($j=1;$j<=$i;$j++)
9. {
10. echo $j.'*'.$i.'='.$i*$j."/t";
11. if($i==$j) echo '
12. ';
13. }
14. }
15. }
16. }
//PHP输出乘法表-循环
function chengfa($a,$step)
{
if( !empty($a) && !empty($step) )
{
for($i=$a;$i<=$step;$i++)
{
for($j=1;$j<=$i;$j++)
{
echo $j.'*'.$i.'='.$i*$j."/t";
if($i==$j) echo '
';
}
}
}
}