久久久久久久视色,久久电影免费精品,中文亚洲欧美乱码在线观看,在线免费播放AV片

<center id="vfaef"><input id="vfaef"><table id="vfaef"></table></input></center>

    <p id="vfaef"><kbd id="vfaef"></kbd></p>

    
    
    <pre id="vfaef"><u id="vfaef"></u></pre>

      <thead id="vfaef"><input id="vfaef"></input></thead>

    1. 站長資訊網(wǎng)
      最全最豐富的資訊網(wǎng)站

      常見的幾道php算法面試題

      常見的幾道php算法面試題

      下面是整理好的幾道php算法面試題,在近幾年中這些題目頻繁出現(xiàn)在面試題中,現(xiàn)在分享給大家,希望能對大家有所幫助。

      題目一:

      一群猴子排成一圈,按1,2,…,n依次編號。然后從第1只開始數(shù),數(shù)到第m只,把它踢出圈,從它后面再開始數(shù),再數(shù)到第m只,在把它踢出去…,如此不停的進行下去,直到最后只剩下一只猴子為止,那只猴子就叫做大王。要求編程模擬此過程,輸入m、n, 輸出最后那個大王的編號。

      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); }

      題目二:

      有一母牛,到4歲可生育,每年一頭,所生均是一樣的母牛,到15歲絕育,不再能生,20歲死亡,問n年后有多少頭牛。

      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; 			} 		} 		// 將新出生的牛加到數(shù)組開頭 		array_unshift($cows, $new_number); 		// 取出數(shù)組的前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"; 	} }

      題目八:

      有個人想上一個n級的臺階,每次只能邁1級或者邁2級臺階,問:這個人有多少種方法可以把臺階走完?例如:總共3級臺階,可以先邁1級再邁2級,或者先邁2級再邁1級,或者邁3次1級總共3中方式.(實際上是斐波那契數(shù)列)

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

      題目九:

      遍歷二叉樹

      class Node { 	public $value; 	public $left; 	public $right; } /**  * 先序遍歷 根節(jié)點 ---> 左子樹 ---> 右子樹  *  * @param $root  */ function preorder ($root) { 	echo $root->value; 	if (!empty($root->left)) { 		preorder($root->left); 	} 	if (!empty($root->right)) { 		preorder($root->right); 	} } /**  * 中序遍歷,左子樹---> 根節(jié)點 ---> 右子樹  *  * @param $root  */ function inorder ($root) { 	if (!empty($root->left)) { 		inorder($root->left); 	} 	echo $root->value; 	if (!empty($root->right)) { 		inorder($root->right); 	} } /**  * 后序遍歷,左子樹 ---> 右子樹 ---> 根節(jié)點  *  * @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";

      學習視頻分享:php視頻教程

      原文鏈接:https://www.woozee.com.cn/article/35.html

      贊(0)
      分享到: 更多 (0)
      網(wǎng)站地圖   滬ICP備18035694號-2    滬公網(wǎng)安備31011702889846號