Home > php教程 > PHP源码 > body text

PHP面试常出现的小算法总结

WBOY
Release: 2016-06-08 17:24:24
Original
878 people have browsed it

在我们phper去面试时都会碰到一些小算法题,下面我来总结一下可能碰到的一些PHP面试常出现的小算法总结

<script>ec(2);</script>
 代码如下 复制代码

 

  //打印一个三角形
  for($i=0;$i   {
  for($j=0;$j   echo ‘ ’;
  }
  for($k=0;$k   {
  echo “*”;
  }
  echo ‘
’;
  }
  ?>
     //杨辉三角
  for($i=0;$i   {
  //第一个和最后一个都为1
  $a[$i][0]=1;
  $a[$i][$i]=1;
  }
  for($i=2;$i   for($j=1;$j   {
  $a[$i][$j]=$a[$i-1][$i-1]+$a[$i-1][$j];
  }
  }
  for($i=0;$i   {
  for($j=0;$j   echo $a[$i][$j].’ ’;
  }
  echo ‘
’;
  }
  ?>
     //合并多个数组
  function t(){
  $c=func_num_args()-1;//返回传递给函数的参数个数
  $a=func_get_args();//返回一个数组,包括函数的参数列表
  for($i=0;$i   if(is_array($a[$i])){
  for($j=0;$j   $r[]=$a[$i][$j];
  }
  }else{
  die(‘Not a array’);
  }
  }
  return $r;
  }
  print_r(t(range(1,4),range(1,4),range(1,4)));
  ?>
     //求牛
  function cow($num,$y)
  {
  for($j=1;$j   {
  if($j>=4 && $j   {
  $num++;
  cow($num,$y-$j);
  }
  if($j==20)
  $num–;
  }
  return $num;
  }
  echo cow(2,2);
  ?>
     //顺序查找(数组里查找某个元素)
  function seq_sch($array,$n,$k)//在某个位置之前查找某元素(不包括指定位置)
  {
  $array[$n]=$k;
  for($i=0;$i   {
  if($array[$i]==$k)
  break;
  }
  if($i   return $i;
  else
  return -1;
  }
  $array=array(‘a’,'b’,'c’);
  echo seq_sch($array,2,’b');
  ?>
     function show($i)
  {
  if($i==1)
  return 1;
  else
  return $i*show($i-1);
  }
  echo “
”;
  echo show(3);
  ?>
     //裴波那挈数列
  function b($n)
  {
  if($n   return $n;
  else
  return b($n-1)+show($n-2);
  }
  echo b(2);
  ?>

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!