dougang6821 2012-08-12 19:12
浏览 38
已采纳

是否存在在数组之间进行选择的方法,该数组具有特定位置的最高值

the roadblock: using multiple nested arrays and wanting to select the array with the largest int in a specific position; in the selected array change another specific position's value.

psuedo code might look like this:

$array1 = array(2, 23, 7);
$array2 = array(2, 21, 7);

$Mutt = array($L, $P, $O, $array1)
$Jeff = array($L, $P, $O, $array2)

find array with max [3][1] {
('selected' [1]++)
}

in real life this might look like: Mutt ($Mutt) has the worked the most days this month ($Mutt [3][1]), he has earned an additional 'personal day' ($Mutt [1]).

I have tried to find a solution to this. I may be too new to understand how to word my search correctly, but I am having no luck.

  • 写回答

2条回答 默认 最新

  • dqcd84732 2012-08-12 19:52
    关注

    I hope I have undestood well what you want:

    Your arrays:

    $array1 = array(2, 23, 7);
    $array2 = array(2, 21, 7);
    
    $Mutt = array($L, $P, $O, $array1);
    $Jeff = array($L, $P, $O, $array2);
    

    Creating a new array $people which contains $Mutt and $Jeff (passed by reference).

    $people=array(&$Mutt,&$Jeff);
    

    Creating function findMaxIndex, which returns the index for which $people[that index] is the array with maximum value at the position that we want.

    Its arguments are:

    • $arr, the array which contains the arrays we want to compare (in this case, $people)
    • $pos1 and $pos2, which are the indexes we want to compare

    So... we will compare

    • $arr[0][$pos1][$pos2]
    • $arr[1][$pos1][$pos2]
    • ...
    • $arr[count($arr)-1][$pos1][$pos2]

    This function works like this:

    1. It creates the array $max, where $max[0] is the index of $arr with the maximum value (among the arrays we have examined until that moment), and $max[1] is that value.
    2. It iterates through all $arr
    3. If it finds that current value ($arr[$i][$pos1][$pos2]) is greater than the maximum value, $max is updated and becomes array($i,$arr[$i][$pos1][$pos2]).
    4. Finally, it returns $max[0], which is the index for which $people[that index] is the array with maximum value at the position that we want.

    The function is:

    function findMaxIndex($arr,$pos1,$pos2){
        $max=array(0,$arr[0][$pos1][$pos2]);
        for($i=1;$i<count($arr);$i++){
            if($arr[$i][$pos1][$pos2]>$max[1]){
                $max=array($i,$arr[$i][$pos1][$pos2]);
            }
        }
        return $max[0];
    }
    

    Then we call the function...

    $maxIndex=findMaxIndex($people,3,1);
    

    ... which gives 0, so the array with maximum value is $people[0] ($Mutt)

    Finally, we increase that array:

    $people[$maxIndex][1]++;
    

    $Mutt and $Jeff are modified too because we passed them by reference.

    In short,

    $array1 = array(2, 23, 7);
    $array2 = array(2, 21, 7);
    $Mutt = array($L, $P, $O, $array1);
    $Jeff = array($L, $P, $O, $array2);
    $people=array(&$Mutt,&$Jeff);
    function findMaxIndex($arr,$pos1,$pos2){
        $max=array(0,$arr[0][$pos1][$pos2]);
        for($i=1;$i<count($arr);$i++){
            if($arr[$i][$pos1][$pos2]>$max[1]){
                $max=array($i,$arr[$i][$pos1][$pos2]);
            }
        }
        return $max[0];
    }
    $maxIndex=findMaxIndex($people,3,1);//gives `0` -> Max is `$people[0]`
    $people[$maxIndex][1]++;
    

    ==============================================

    And if you want multiple indexes in case of tie (changes in bold):

    Your arrays:

    $array1 = array(2, 23, 7);
    $array2 = array(2, 21, 7);
    
    $Mutt = array($L, $P, $O, $array1);
    $Jeff = array($L, $P, $O, $array2);
    

    Creating a new array $people which contains $Mutt and $Jeff

    $people=array(&$Mutt,&$Jeff);
    

    Creating function findMaxIndex, which returns the array which contains the indexes for which $people[that index] is the array with maximum value at the position that we want.

    Its arguments are:

    • $arr, the array which contains the arrays we want to compare (in this case, $people)
    • $pos1 and $pos2, which are the indexes we want to compare

    So... we will compare

    • $arr[0][$pos1][$pos2]
    • $arr[1][$pos1][$pos2]
    • ...
    • $arr[count($arr)-1][$pos1][$pos2]

    This function works like this:

    1. It creates the array $max, where $max[0] is an array which contains the indexes of $arr with the maximum value (among the arrays we have examined until that moment), and $max[1] is that value.
    2. It iterates through all $arr
    3. If it finds that current value ($arr[$i][$pos1][$pos2]) is greater than the maximum value, $max is updated and becomes array(array($i),$arr[$i][$pos1][$pos2]).
    4. If not, and if it finds that current value ($arr[$i][$pos1][$pos2]) equals the maximum value, $max[0] is updated and $i is pushed into it.
    5. Finally, it returns $max[0], which is the array which contains the indexes for which $people[that index] is the array with maximum value at the position that we want.

    The function is:

    function findMaxIndex($arr,$pos1,$pos2){
        $max=array(array(0),$arr[0][$pos1][$pos2]);
        for($i=1;$i<count($arr);$i++){
            $current=$arr[$i][$pos1][$pos2];
            if($current>$max[1]){
                $max=array(array($i),$current);
            }else if($current==$max[1]){
                array_push($max[0],$i);
            }
        }
        return $max[0];
    }
    

    Then we call the function...

    $maxIndex=findMaxIndex($people,3,0);
    

    ... which gives array(0,1), so the arrays with maximum value are $people[0] ($Mutt) and $people[1] ($Jeff).

    Finally, we increase the arrays:

    for($i=0;$i<count($maxIndex);$i++){
        $people[$maxIndex[$i]][1]++;
    }
    

    $Mutt and $Jeff are modified too because we passed them by reference.

    In short,

    $array1 = array(2, 23, 7);
    $array2 = array(2, 21, 7);
    $Mutt = array($L, $P, $O, $array1);
    $Jeff = array($L, $P, $O, $array2);
    $people=array(&$Mutt,&$Jeff);
    function findMaxIndex($arr,$pos1,$pos2){
        $max=array(array(0),$arr[0][$pos1][$pos2]);
        for($i=1;$i<count($arr);$i++){
            $current=$arr[$i][$pos1][$pos2];
            if($current>$max[1]){
                $max=array(array($i),$current);
            }else if($current==$max[1]){
                array_push($max[0],$i);
            }
        }
        return $max[0];
    }
    $maxIndex=findMaxIndex($people,3,0);//gives `array(0,1)` -> Tie between `$people[0]` and `$people[1]`
    for($i=0;$i<count($maxIndex);$i++){
        $people[$maxIndex[$i]][1]++;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c