如何获得最大连续+值?

时间:2018-10-01 11:12:37

标签: php

$variable中有很多值,它们具有-ve和+ ve随机值。如何计算最大连续正值值?例如

 $variable = array:12 [▼
              0 => 258300
              1 => 668000
              2 => -1510530
              3 => 15000
              4 => 2400
              5 => 13400
              6 => 284000
              7 => -45000000
              8 => 7209702
              9 => 1000074080
              10 => 0
              11 => 1100
            ]

在上述情况下,输出应类似于:4具有max constructiveness of positive值。因为,

              3 => 15000
              4 => 2400
              5 => 13400
              6 => 284000

这些值具有+ ve连续值,并且其最大连续值。

3 个答案:

答案 0 :(得分:1)

  • 创建一些变量以存储一些临时值
  • 环绕$variable,并确定它是否处于正趋势。正趋势表示先前值和当前值都为正。
  • 将其存储在当前的“积极”趋势中,并检查是否违反了先前的最大连续趋势。如果被破坏,则可以将当前趋势设置为新的最大趋势。

尝试以下代码:

// Define a variable which stores the previous value
$prev = null;
$current_positive_trend = array();
$max_consecutive_positive_trends = array();

foreach ($variable as $value) {

    /* For PHP 5 you can do the following instead
    $prev = isset($prev) ? $prev : $value;
    */
    $prev = ($prev ?? $value); // first time consider current value

    // if current value and previous value is greater than zero
    if ($prev > 0 && $value > 0) {

        // add the value to current positive trend
        $current_positive_trend[] = $value;

        // check if the count of current positive trend array is more than
        // the max consecutive positive trends found till now
        if (count($current_positive_trend) > count($max_consecutive_positive_trends)) {

            // set current trend to max consecutive trends
            $max_consecutive_positive_trends = $current_positive_trend;
        }
    } else {

        // not in positive trend - reset
        $current_positive_trend = array();
    }
}

$max_times_consecutive_positive = count($max_consecutive_positive_trends);

// print the consecutive positive values (in max case
var_dump($max_consecutive_positive_trends);
// print the max times trend was positive.
echo $max_times_consecutive_positive;

答案 1 :(得分:1)

您想用最少的代码完成它吗?

<?
$v = [
    258300,
    668000,
    -1510530,
    15000,
    2400,
    13400,
    284000,
    7209702,
    1000074080,
    0,
    1100,
    45,
    -1
];
$m=0;
$c=0;

foreach ($v as $i) {$c=($i>0)?((++$c>$m)?($m=$c):$c):0;};

var_dump($m);

答案 2 :(得分:1)

这是代码。您应该优化代码。我添加了一些评论,可帮助您进行其他更改

您可以检查所需的output here

    <?php

    $data=array(258300,668000,-1510530,15000,2400,13400,284000,-45000000,7209702,1000074080,0,1100);
    $bak_index=array();
    $bak=array();
    $index_start=0;
    // print_r(count($data));
    // exit;
    for($i=0;$i<count($data);$i++){


        if($data[$i] <=0){
            //echo $i.'='.$data[$i].'__'; for testing
            $index_end=$i;
            $bak_index['start']=$index_start;//get start index
            $bak_index['end']=$index_end;//get end index number
            $bak_index['total']=$bak_index['end']-$bak_index['start']; //total positive number index
            $index_start=$i+1;
            $bak[]=$bak_index;
        }


    }
    echo "<pre>";
    print_r($bak);//here is array with start index, end index & total  positive consecutive value
    $max_value=max(array_column($bak, 'total'));
    //print_r($max_value);
 //now get values from $data varaible using array_slice function   
    for ($i=0; $i < count($bak) ; $i++) {
        if($bak[$i]['total'] == $max_value){
            echo $bak[$i]['start'];
            echo "_____";
            echo $bak[$i]['end'];
            echo "<br>";
            $value[]= array_slice($data, $bak[$i]['start'], $bak[$i]['total']);
        }

    }
    print_r($value);