首页 > 代码库 > 2nd day

2nd day

技术分享

<?php
//求数组的平均值
    $a3 = array(
    array(11,12, 13),
    array(21,22,23, 24, 25),
    array(31,32,33, 35),
    array(41,42,43)
);
    $length = count($a3);
    $count = 0;
    $sum = 0;
    for($i=0; $i<$length; ++$i){
            $length2 = count($a3[$i]);
            for($j=0; $j<$length2; ++$j){
                    $sum += $a3[$i][$j];
                    ++$count;
                }    
        }
    $average = $sum / $count;
    echo "数组的平均值是:$average";
    echo "<hr>";

    //一个“不整齐”数字数组,如下所示,求其平均值:
    $a4 = array(
    1,
    array(21,22,23, 24, 25),
    3,
    array( 41, 42, 43,array(50,  51,  52) )
);
    $sum = 0;
    $count = 0;
    function digui($arr){
        $length = count($arr);
        for($i=0; $i<$length; ++$i){
            $length2 = count($arr[$i]);
            if($length2==1){
                $GLOBALS[‘sum‘] += $arr[$i];
                ++$GLOBALS[‘count‘];
            }
            else{
                 digui($arr[$i]);
            }
        }
        return $GLOBALS[‘sum‘]/$GLOBALS[‘count‘];
    }
    $a = digui($a4);
    echo $a;
    echo ‘<hr>‘;

    //求一个整数数组中的最小的奇数,如果没有奇数,则直接输出“没有奇数”,否则输出该数。
    $arr = array(56,76,4,98,6,3,5);
    $length = count($arr);
    for($i=0; $i<$length; ++$i){
        if($arr[$i]%2!=0){
            $arr2[]=$arr[$i];
        }
    }
    if(!empty($arr2)){
        $min = $arr2[0];
        for($j=0; $j<count($arr2); ++$j){
            if($min>$arr2[$j]){
                $min = $arr2[$j];
            }
        }
        echo "最小的奇数为:$min";
    }
    else{
        echo "大哥,没有奇数!";
    }
    
?>

 

2nd day