﻿ 常见的几道php算法面试题 - 鸿网互联

# 常见的几道php算法面试题

```function king(\$m, \$n) {
if (1 >= \$n) {
return \$n;
}
\$monkeys = range(1, \$n);
\$count = \$n;
while (\$count > 1) {
\$remainder = \$m % \$count;
unset(\$monkeys[\$remainder - 1]);
\$monkeys = array_values(\$monkeys);
\$count--;
}
return array_shift(\$monkeys);
}```

```function cows (\$n) {
\$cows = [1];
for (\$i = 1; \$i &lt;= \$n; \$i++) {
// 新出生的牛
\$new_number = 0;
foreach (\$cows as \$age => \$num) {
// 4岁到14岁的牛生育新的母牛
if (\$age >= 3 &amp;
&amp;
\$age &lt;
= 13) {
\$new_number += \$num;
}
}
// 将新出生的牛加到数组开头
array_unshift(\$cows, \$new_number);
// 取出数组的前20个单元
\$cows = array_slice(\$cows, 0, 20);
}
return array_sum(\$cows);
}```

```function bubble_sort (\$array) {
\$array = array_values(\$array);
for (\$i = 0; \$i &lt; count(\$array); \$i++) {
for (\$j = 0;\$j &lt; count(\$array) - \$i - 1; \$j++) {
if (\$array[\$j] > \$array[\$j + 1]) {
\$temp = \$array[\$j + 1];
\$array[\$j + 1] = \$array[\$j];
\$array[\$j] = \$temp;
}
}
}
return \$array;
}```

```function quick_sort (\$array) {
if (count(\$array) &lt;
= 1) {
return \$array;
}
\$left_array = [];
\$right_array = [];
\$key = array_shift(\$array);
foreach (\$array as \$value) {
if (\$key > \$value) {
\$left_array[] = \$value;
} else {
\$right_array[] = \$value;
}
}
return array_merge(quick_sort(\$left_array), [\$key], quick_sort(\$right_array));
}```

（学习视频分享：php视频教程）

```function select_sort (\$array) {
\$sort_array = [];
while (count(\$array)) {
\$min = null;
\$min_key = null;
foreach (\$array as \$key => \$value) {
if (is_null(\$min)) {
\$min = \$value;
\$min_key = \$key;
} elseif (\$min > \$value) {
\$min = \$value;
\$min_key = \$key;
}
}
\$sort_array[] = \$min;
unset(\$array[\$min_key]);
}
return \$sort_array;
}```

```function unique_char (\$str) {
\$arr = array_unique(str_split(\$str));
sort(\$arr);
return implode('', \$arr);
}```

```function all_file (\$dir) {
if (is_dir(\$dir)) {
\$resource = opendir(\$dir);
while (\$file = readdir(\$resource)) {
if (in_array(\$file, ['.', '..'])) {
continue;
} elseif (is_dir(\$dir . '/' . \$file)) {
all_file(\$dir . '/' . \$file);
} else {
echo \$dir . '/' . \$file, "\n";
}
}
} else {
echo \$dir, "\n";
}
}```

```function ladder(\$steps) {
return \$steps &lt;
2 ? 1 : ladder(\$steps - 1) + ladder(\$steps - 2);
}```

```class Node {
public \$value;
public \$left;
public \$right;
}
/**
* 先序遍历 根节点 ---> 左子树 ---> 右子树
*
* @param \$root
*/
function preorder (\$root) {
echo \$root->value;
if (!empty(\$root->left)) {
preorder(\$root->left);
}
if (!empty(\$root->right)) {
preorder(\$root->right);
}
}
/**
* 中序遍历，左子树---> 根节点 ---> 右子树
*
* @param \$root
*/
function inorder (\$root) {
if (!empty(\$root->left)) {
inorder(\$root->left);
}
echo \$root->value;
if (!empty(\$root->right)) {
inorder(\$root->right);
}
}
/**
* 后序遍历，左子树 ---> 右子树 ---> 根节点
*
* @param \$root
*/
function tailorder (\$root) {
if (!empty(\$root->left)) {
tailorder(\$root->left);
}
if (!empty(\$root->right)) {
tailorder(\$root->right);
}
echo \$root->value;
}
\$d = new Node;
\$d->value = 'D';
\$b = new Node;
\$b->value = 'B';
\$b->left = \$d;
\$e = new Node;
\$e->value = 'E';
\$f = new Node;
\$f->value = 'F';
\$c = new Node;
\$c->value = 'C';
\$c->left = \$e;
\$c->right = \$f;
\$a = new Node;
\$a->value = 'A';
\$a->left = \$b;
\$a->right = \$c;
preorder(\$a);
echo "\n";
inorder(\$a);
echo "\n";
tailorder(\$a);
echo "\n";```

<