PHP面试题

1、计算数组中元素的值 。

$arr = [2,4,5,6,7,23,45,12,1];

$sum = 0;
function fun($sum,$arr){
    global $sum;
    if(!empty($arr)){
        $sum += array_pop($arr);
        fun($sum,$arr);
    }

    return $sum;
}
fun($sum,$arr);
var_dump( $sum );

 2、写出以下代码输出结果?

  $data=array('a','b','c');
  foreach($data as $key=>$value){
       $value=&$data[$key];
  }

3、计算数组中第N个元素的值。()

$arr = [0,1,1,2,3,5,8,13,21];

function fun($n,$arr){
    $fn = $arr[$n-1] + $arr[$n-2];
    return $fn;
}

$fn =  fun(5,$arr);
echo $fn;

4、某人每次可以上一个台阶或两个台阶,问上到第65个台阶有多少种方法?

$arr = range(1,65);
$count =0;
$match_arr = [];
foreach($arr as $key=>$val){
    foreach($arr as $v){
        if($val + $v ==65){
            $count++;
            $match_arr[$val] = $v;
            continue;
        }
    }
}

5、约瑟夫环问题

方案一、操作指针

function getJoseph($n, $m)
{
    $arr = range(1,$n);//给每个人设置编号

    while(count($arr)>1)
    {
        for($i = 1; $i <= $m; $i++)
        {
            if(next($arr)){ //如果存在 next 元素
                if($i == $m)
                {
                    $k = array_search(prev($arr), $arr);//查找当前数组的key
                    unset($arr[$k]);//当数到 m 时,使用 unset() 删除数组元素
                    unset($k);
                }
            }
            else{
                reset($arr);//则数组的第一个元素充当 next 元素,组成一个环
                if($i == $m)
                {
                    $k =array_search(end($arr), $arr);
                    unset($arr[$k]);//当数到 m 时,使用 unset() 删除数组元素,注意这里是 end()
                    reset($arr);//指针复位
                    unset($k);
                }
            }
        }
    }
    return current($arr);
}

echo getJoseph(100, 3);

6、$str="eeeffffgghhhhhhhjjjjjjjjjj"; //要求输出 3e4f2g7h10

function fun($str){
    $arr = str_split($str);

    $new_str = '';
    $m = 1;
    for($i=0;$i<count($arr);$i++){
        if(isset( $arr[$i+1] )){
            if( $arr[$i] == $arr[$i+1] ){
                $m++;
            }else{
                $new_str .= $m.$arr[$i];
                $m=1;
            }
        }else{
            $new_str .= $m.$arr[$i];
        }
    }
    return $new_str;
}

7、无限级分类

$arr = [
    [
        'cat_id'=>1,
        'parent_cat_id'=>0
    ],
    [
        'cat_id'=>2,
        'parent_cat_id'=>1
    ],
    [
        'cat_id'=>3,
        'parent_cat_id'=>1
    ],
    [
        'cat_id'=>4,
        'parent_cat_id'=>2
    ],
    [
        'cat_id'=>5,
        'parent_cat_id'=>4
    ],
    [
        'cat_id'=>6,
        'parent_cat_id'=>0
    ],
    [
        'cat_id'=>7,
        'parent_cat_id'=>6
    ]
];
function children($id,$arrs){
    $result =[];
    foreach ($arrs as $v){
        if($id==$v['parent_cat_id']){
            $result[]=$v;
        }
    }
    return $result;
}
function allChildren($id,$arrs){

    $result = [];
    $children = children($id,$arrs);//获取儿子数组
    foreach($children as $k=>$child){
        $result[$k]=$child;
        $childResult = allChildren($child['cat_id'],$arrs);//获取儿子的儿子的儿子无穷尽也
        foreach ($childResult as $subChild) {
            $child['children'][]=$subChild;
            $result[$k] = $child;
        }
    }
    return $result;
}
echo '<pre>';print_r( allChildren(0,$arr) );

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值