# 计算向量的有界累积和

19

``````vec =     [1  1  1 -1 -1 -1 -1 -1  1  1  1  1 -1]
cum_vec = [1  2  3  2  1  0 -1 -2 -1  0  1  2  1]
``````

``````upper_lim = 2
lower_lim = -1
vec =     [1  1  1 -1 -1 -1 -1 -1  1  1  1  1 -1]
cum_vec = [1  2  2  1  0 -1 -1 -1  0  1  2  2  1]
``````

``````upper_lim = 6
lower_lim = -2
vec =     [1  4  3 -10  3  2  2  5 -4]
cum_vec = [1  5  6  -2  1  3  5  6  2]

upper_lim = 100
lower_lim = -100
vec =     [1  1  1  1  1  1]
cum_vec = [1  2  3  4  5  6]

upper_lim = 5
lower_lim = 0
vec =     [10 -4 -3  2]
cum_vec = [5   1  0  2]

upper_lim = 0
lower_lim = 0
vec =     [3  5 -2  1]
cum_vec = [0  0  0  0]

upper_lim = 10
lower_lim = 5
vec =     [1  4  6]
cum_vec = [5  9 10]
|
Note, jumped to 5, because 5 is the lower bound.
``````

5

# Pyth，14个字节

``````t.u@S+Q+NY1vwZ
``````

### 说明

``````t.u@S+Q+NY1vwZ  implicit: Q = first input list [upper_lim, lower_lim]
.u        vwZ  for each number Y in the next input list, update N = 0 with:
+NY         N + Y
+Q            append this to Q
S              sort this list
@      1        take the middle element
.u returns a list with all intermediate values of N
t                  remove the first value, print the rest
``````

5

# CJam，16 15字节

``````l~f{\T++\$1=:T}`
``````

``````[1 4 3 -10 3 2 2 5 -4] [6 -2]
``````

``````l~    Get and parse input. This leaves the value and bounds lists on the stack.
f{    Apply block with value (the bounds list).
\     Swap new value to top.
T     Get previous value from variable T (which is default initialized to 0).
+     Add new value and previous value.
+     Append new value to bounds list, producing a 3 value list.
\$     Sort it...
1=    And take the middle value.
:T    Store in variable T for next iteration.
}     End of apply loop.
`     Convert list to string.
``````

4

# JavaScript（ES6），43个字节

``````(l,u,v,p=0)=>v.map(c=>p=(p+=c)<l?l:p>u?u:p)
``````

4

``````u#l=tail.scanl(((min u.max l).).(+))0
``````

3

## R，61字节

``````function(x,l,u,s=0)sapply(x,function(i)s<<-min(u,max(l,s+i)))
``````

`sapply`是将功能应用于向量的每个元素（此处`x`）的函数，但通常是在所有评估都是独立且无副作用的情况下完成的。但是，在这里，我使用`<<-`运算符在的父/调用环境中进行赋值，`sapply`以便可以将累积和`s`存储在迭代评估之外。这是非常糟糕的做法...

3

# Mathematica，46个字节

``````Rest@FoldList[{a,b}Min[a+b,#2]~Max~#3,0,#]&
``````

3

# 朱莉娅44 42 38字节

``f(x,l,u,s=0)=[s=clamp(s+i,l,u)for i=x]``

``````function f(v::Array, u::Int, l::Int, s::Int = 0)
# The parameter s is the cumulative sum, which begins
# at 0

# For each element i of v, define s to be s+i if
# l ≤ s+i ≤ u, l if s+i < l, or u if s+i > u
x = [s = clamp(s + i, l, u) for i = v]

return x
end``````

3

# Python 2，67字节

``````lambda u,l,v:reduce(lambda x,y:x+[max(min(x[-1]+y,u),l)],v,[0])[1:]
``````

2

## Minkolang 0.9，30个字节

``````0I3-[2g+d0c`,3&x0cd1c`3&x1cdN]
``````

``````(n\$I\$)0I4-[2g+d0c`,3&x0cd1c`3&x1cdN].
``````

### 说明

``````(n\$I\$)                                   Read in integers from input until empty
0                                  Initialize cumulative sum
I4-[                        ]     Loop over vector
2g+                           Get the next partial sum
d0c`,3&x0c                 If too high, replace with high
d1c`3&x1cd       If too low, replace with low
N      Output as integer
.    Stop
``````

1

### C 98字节

``````#define P printf(
void c(*v,n,u,l,s,c){P"[");while(c++<n)s+=*v++,s=s<u?s>l?s:l:u,P"%d ",s);P"]");}``````

### 使用范例

``````#define P printf(
void c(*v,n,u,l,s,c) {
P"[");
while(c++<n)
s+=*v++,s=s<u?s>l?s:l:u,P"%d ",s);
P"]");
}

int main() {
int vec[9] = {1, 4, 3, -10, 3, 2, 2, 5, -4};
int upper = 6, lower = -2, count = 9;
c(vec, count, upper, lower, 0, 0);
}``````

``[1 5 6 -2 1 3 5 6 2 ]``

1

# APL，29 27 18字节

``````1↓(⎕⌈⎕⌊+)/¨⌽¨,\0,⎕
``````