Php 我如何才能将其导出最大连续+;价值
Php 我如何才能将其导出最大连续+;价值,php,Php,$variable中有很多值,它们具有随机的-ve和+ve值。如何计算最大连续正值?比如说, $variable = array:12 [▼ 0 => 258300 1 => 668000 2 => -1510530 3 => 15000 4 => 2400 5 => 13400
$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具有正的最大构造性值。因为
3 => 15000
4 => 2400
5 => 13400
6 => 284000
这些值具有continue+ve值及其最大连续值。- 创建一些变量来存储一些临时值
- 在
$变量上循环,并确定它是否处于正趋势。正趋势意味着以前的值和当前值都是正的
- 将其存储在当前正向趋势中,并检查您以前的最大连续趋势是否被破坏。如果它被破坏,您可以将当前趋势设置为新的最大趋势
// 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;
- 创建一些变量来存储一些临时值
- 在
$变量上循环,并确定它是否处于正趋势。正趋势意味着以前的值和当前值都是正的
- 将其存储在当前正向趋势中,并检查您以前的最大连续趋势是否被破坏。如果它被破坏,您可以将当前趋势设置为新的最大趋势
// 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;
你想用最少的代码完成吗
<?
$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);
您希望用最少的代码完成吗
<?
$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);
这是代码。您应该优化代码。我添加了一些注释,可以帮助您进行其他更改
你可以检查你想要的
这是代码。您应该优化代码。我添加了一些注释,可以帮助您进行其他更改
你可以检查你想要的
到目前为止您尝试了什么?到目前为止您尝试了什么?