A summary of the small algorithms often found in PHP interviews

Source: Internet
Author: User
Tags echo b
The code is as follows Copy Code

<?php
Print a triangle
For ($i =0 $i <=3; $i + +)
{
for ($j =0; $j <=3-$i; $j + +) {
Echo ' &nbsp; ';
}
for ($k =0; $k <=2* $i; $k + +)
{
echo "*";
}
Echo ' <br> ';
}
?>
<?php
Yang Hui's triangle
For ($i =0 $i <6; $i + +)
{
The first and last are 1.
$a [$i][0]=1;
$a [$i] [$i]=1;
}
For ($i =2 $i <6; $i + +) {
for ($j =1; $j < $i; $j + +)
{
$a [$i] [$j]= $a [$i -1][$i -1]+ $a [$i -1][$j];
}
}
For ($i =0 $i <6; $i + +)
{
for ($j =0; $j <= $i; $j + +) {
echo $a [$i] [$j]. ' &nbsp; ';
}
Echo ' <br/> ';
}
?>
<?php
Merging multiple arrays
function T () {
$c =func_num_args () -1;//returns the number of arguments passed to a function
$a =func_get_args ()//returns an array, including the parameter list of the function
for ($i =0; $i <= $c; $i + +) {
if (Is_array ($a [$i])) {
For ($j =0 $j <count ($a [$i]); $j + +) {
$r []= $a [$i] [$j];
}
}else{
Die (' not a array ');
}
}
return $r;
}
Print_r (t (range (1,4), Range (1,4), Range (1,4));
?>
<?php
Begging cattle
Function Cow ($num, $y)
{
for ($j =1; $j < $y; $j + +)
{
if ($j >=4 && $j <15)
{
$num + +;
Cow ($num, $y-$j);
}
if ($j ==20)
$num –;
}
return $num;
}
Echo Cow (2,2);
?>
<?php
Order lookup (Find an element in an array)
function Seq_sch ($array, $n, $k)//Find an element before a location (excluding the specified location)
{
$array [$n]= $k;
for ($i =0; $i < $n; $i + +)
{
if ($array [$i]== $k)
Break
}
if ($i < $n)
return $i;
Else
return-1;
}
$array =array (' A ', ' B ', ' C ');
Echo Seq_sch ($array, 2, ' B ');
?>
<?php
Function Show ($i)
{
if ($i ==1)
return 1;
Else
return $i *show ($i-1);
}
echo "<br>";
Echo Show (3);
?>
<?php
Peibo that pull out series
Function B ($n)
{
if ($n <2)
return $n;
Else
Return B ($n-1) +show ($n-2);
}
Echo b (2);
?>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.