用PHP解决序列方程

时间:2014-09-02 01:44:39

标签: php math sequence

伙计们如何在PHP中解决这种排序方程式:

$D = array(120, 30, 10, 55...);
S1=D1
S2=D1-D2
S3=D1-D2-D3

1 个答案:

答案 0 :(得分:1)

我认为你正在寻找类似的东西:

$D = array(120, 30, 10, 55);
$S[] = array_shift($D);

foreach ($D as $k => $v) {
    $S[$k + 1] = $S[$k] - $v;
}

print_r($S);

输出:

Array
(
    [0] => 120
    [1] => 90
    [2] => 80
    [3] => 25
)