<code class="lang-php"><br> <?php
function merge($frist=array(),$second=array(),$attribute=''){
$key=0;
$c=0;
$d=0;
$temp=array();
for($a=0;$a<count($frist);$a++){
for($b=$c;$b<count($second);$b++){
if($frist[$a]<=$second[$b]){
$d++;
$temp[$key]=$frist[$a];
$key++;
break;
}
else{
$c++;
$temp[$key]=$second[$b];
$key++;
}
}
}
if($frist[$d-1]<=$second[$c-1]){
for($d;$d<count($frist);$d++){
array_push($temp, $frist[$d]);
}
}
else{
for($b;$b<count($second);$b++){
array_push($temp, $second[$b]);
}
}
return $temp;
}
$f=array(1,2,3,4,5,6,9,10,100,101);
$s=array(1,2,3,4,8,20,21,22,300);
$sf=merge($f,$s);
print_r($sf);
</code>这个问题已被关闭,原因:无法获得确切结果的问题
<code class="lang-php"><br> <?php
function merge($frist=array(),$second=array(),$attribute=''){
$key=0;
$c=0;
$d=0;
$temp=array();
for($a=0;$a<count($frist);$a++){
for($b=$c;$b<count($second);$b++){
if($frist[$a]<=$second[$b]){
$d++;
$temp[$key]=$frist[$a];
$key++;
break;
}
else{
$c++;
$temp[$key]=$second[$b];
$key++;
}
}
}
if($frist[$d-1]<=$second[$c-1]){
for($d;$d<count($frist);$d++){
array_push($temp, $frist[$d]);
}
}
else{
for($b;$b<count($second);$b++){
array_push($temp, $second[$b]);
}
}
return $temp;
}
$f=array(1,2,3,4,5,6,9,10,100,101);
$s=array(1,2,3,4,8,20,21,22,300);
$sf=merge($f,$s);
print_r($sf);
</code>
一般我看到这种代码面试第一关就通不过。first拼错、代码格式不规范、$f,$s,$sf是什么东西?这种代码首先就不是容易让人看懂,何谈维护和理解
有点晕,看了一会发现,好像合并过程中带着冒泡排序的意思
<code class="lang-php">$arrayA=array(1,3,2,4,5,6,9,10,100,101);
$arrayB=array(1,2,3,103,7,8,20,21,22,300);
$arrayMerge=array_merge($arrayA,$arrayB);
function bubbleSort($array){
$count=count($array);
for($i=0;$i<$count-1;$i++){//循环比较
for($j=$i+1;$j<$count;$j++){
if($array[$j]<array[$i]){//执行交换
$temp=$array[$i];
$$array[$i]=$array[$j];
$array[$j]=$temp;
}
}
}
return$array;
}
var_dump(bubbleSort($arrayMerge));
</code>
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号