search
Homephp教程PHP源码PHP面试常出现的小算法总结

在我们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);
  ?>

Statement
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

DVWA

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

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool