时间:2021-07-01 10:21:17 帮助过:15人阅读
代码如下:
$list = array(1,-3,-5,-7,8,9,-11,5);
$cur = 0;
$term = 0;
$res = 0;
$begin = 0;
foreach($list as $k => $v){
$cur += $v;
if($cur < 0){
$cur = 0;
$begin = $k + 1;
}
if($cur > $res){
$res = $cur;
$term = $k;
}
}
$max_seq = array_slice($list, $begin, ($term - $begin) + 1);
echo $res . ',';
print_r($max_seq);
//17,Array ( [0] => 8 [1] => 9 )