一个简单的计步器可以用一个摆来模拟,它的两侧有两个开关--一个在x=0,一个在x=l。当钟摆碰到远处的开关时,救护车可以被假定已经走了半步。当它接触到接近开关时,该步骤就完成了。
给定一个代表钟摆位置的整数列表,确定计步器上记录的完整步数。
所测量的完整步骤数。当摆接触远开关(x>=l)和近开关(x<=0)时,就采取了一个步骤。
8, [8, 3, 0, 1, 0, 2, 2, 9, 4, 7]
1钟摆立即与位于x=8 at t=0的远开关进行接触。然后,它触及t=2和t=4的近开关,完成了一步。在此之后,它再次触及x=9 at t=8的far开关,但它再也没有触及近开关。
1, [1, 5, -1, -4, -1, 1, -2, 8, 0, -4]
3
15, [10, -7, -13, 19, 0, 22, 8, 9, -6, 21, -14, 12, -5, -12, 5, -3, 5, -15, 0, 2, 11, -11, 12, 5, 16, 14, 27, -5, 13, 0, -7, -2, 11, -8, 27, 15, -10, -10, 4, 21, 29, 21, 2, 5, -7, 15, -7, -14, 13, 27]
7
7, [5, 4, 0]
0
7, [5, 8, 6, 1, 2]
0 发布于 2016-03-08 22:23:04
>~2G0>~-XzY'nw1)0<-H/k这使用了语言/编译器的现行版本(14.0.0)。
输入与挑战的顺序和格式相同,由换行符分隔。
>~ % take the two inputs implicitly. Generate an array that contains true
% where the second input (array of positions) is >= the first input (l)
2G % push second input (positions) again
0 % push a 0
>~ % true where the second input (array of positions) is <= 0
- % subtract: array of 1, -1 or 0
Xz % discard the 0 values
Y' % run-length encoding: push values and number of repetitions
n % length of the latter array: number of half-steps, perhaps plus 1
w1) % swap. Get first element
0< % true if that element is -1. We need to discard one half-step then
- % subtract
H/k % divide by 2 and round down发布于 2016-03-08 21:23:06
(t,a)=>a.map(a=>a<t?a>0?'':0:1).join``.split`10`.length-1谢谢@NotThatCharles为-4
发布于 2016-03-09 17:19:35
/.:@J,Q0m@S+Jd1E2J解释:
/.:@J,Q0m@S+Jd1E2J
Implicit: Q is the length of the track.
J,Q0 Set J to [Q, 0]
m E Map over the list
+Jd Add the current element to J
S Sort
@ 1 Take the middle element.
This is the current element,
clamped above by Q and below by 0.
@J Filter for presence in J.
.: 2 Form 2 element substrings
/ J Count occurrences of J.https://codegolf.stackexchange.com/questions/75130
复制相似问题