有一架直升机在离地面500米处飞行。它可以飞到100米,这需要1.0秒。或者,如果它不向上飞,它将下降200米,这也需要1.0秒--这意味着如果指示不是“飞行”,直升机就会坠落。建立一个程序,接受输入的指令列表,告诉直升机什么时候飞,什么时候不飞。当直升机坠落时,它应该输出时间计数器。
投入:
飞舞飞高尔夫球飞飞
产出:
8.5秒
演练:
1)飞==苍蝇so +100 =600 m2)舞蹈!=飞so -200 =400 m 3)飞== Fly so +100 =500 m 4)高尔夫!=飞so -200 m=300 m 5)飞== Fly so +100 =400 m 6) == Fly so +100 =500 m 7)没有指令so -200 =300 m 8)没有指令so -200=100 m 9)没有指令so -100 = 0m (这需要0.5秒)
每条指令都应该用空格分隔,如果安装不正常,则自动下降。当程序运行完指令时,自动下降。您的程序必须输出(浮点)秒,如果需要=> 60.0秒,以mins和秒(例如)显示。1分钟32.0秒
秒是浮动十进制值,分钟是整个中间值。另外,大写化并不重要,你的程序应该是大写还是不大写。
这是代码-高尔夫,所以至少没有字节赢!
发布于 2020-06-22 14:28:59
l#ā«'¤†Q3*Í5šηO.γd}нRć;sg+60‰0Û跟着这封信的挑战。输入为空格分隔的字符串,输出为list [sec]或[min,sec],其中sec是浮点数,潜在的min是整数。
如果允许将输入作为字符串列表,则可以删除#。
如果允许输出为[min,sec],即使min为0,则可以删除尾迹0Û。
l # Convert the (implicit) input-string to lowercase
# i.e. "Fly Dance Fly Golf FLY fLy"
# → "fly dance fly golf fly fly"
# # Split it on spaces
# → ["fly","dance","fly","golf","fly","fly"]
ā # Push a list in the range [1, length]
# (without popping the list itself)
# → [1,2,3,4,5,6]
« # Merge it to the list of words
# → ["fly","dance","fly","golf","fly","fly",1,2,3,4,5,6]
'¤† '# Push dictionary string "fly"
Q # Check for each word (or integer) if it's equal to "fly"
# (1 if truthy; 0 if falsey)
# → [1,0,1,0,1,1,0,0,0,0,0,0]
3* # Multiply each by 3
# → [3,0,3,0,3,3,0,0,0,0,0,0]
Í # Decrease each by 2 (so 1 if truthy; -2 if falsey)
# → [1,-2,1,-2,1,1,-2,-2,-2,-2,-2,-2]
5š # Prepend a 5 to this list
# → [5,1,-2,1,-2,1,1,-2,-2,-2,-2,-2,-2]
η # Get all prefixes of this list
# → [[5],[5,1],...[[5,1,-2,1,-2,1,1,-2,-2,-2,-2,-2,-2]]
O # Sum each prefix
# → [5,6,4,5,3,4,5,3,1,-1,-3,-5,-7]
.γ } # Consecutive group the values by:
d # Where the value is >= 0
# → [[5,6,4,5,3,4,5,3,1],[-1,-3,-5,-7]]
}н # After the group-by, only leave the first group
# → [5,6,4,5,3,4,5,3,1]
Rć # Reverse and extract the head,
# so we've extracted the last item loose to the stack
# → [3,5,4,3,5,4,6,5] and 1
; # Halve that last item
# → [3,5,4,3,5,4,6,5] and 0.5
sg # Swap to get the list, and pop and push its length
# → 8
+ # Add it to the halved last item
# → 8.5
60‰ # Take the divmod-60
# → [0,8.5]
0Û # And remove a potential leading 0
# → [8.5]
# (after which the result is output implicitly)看这个05AB1E我的尖端(部分)如何使用字典?)来理解为什么'¤†是"fly"。
发布于 2016-11-17 04:39:22
版本
i,h=5;main(c,v)int**v;{for(;h>0;)h+=*v[(++i<c)*i]-'ylF'?-2:1;printf("%f secs\n",i-(h<0)*.5);}发布于 2016-11-18 22:51:09
(s=ToString;t=Length[#[[;;(FirstPosition[#,x_/;x<=0,1]-1/. 0->Length@#)]]]&[x=500+Accumulate[If[#=="Fly",100,-200]&/@(StringSplit@#)]];
If[#>=60,s@Floor[#/60]~~" min ",""]~~s@Mod[#,60]~~ " sec"&[t+x[[t]]/200.])&该算法假设飞行结束时,高度首次达到0。看看其他的答案,我想人们会让直升机飞到地面以下。我也不允许“触摸和离开”。
未高尔夫球:
in="Fly Dance Fly Golf Fly Fly";
s=ToString;StringSplit@in
If[#=="Fly",100,-200]&/@%
altList=500+Accumulate@%
l=Length@altList
(* find last positive before first nonpositive *)
FirstPosition[altList,x_/;x<=0,1]-1
t=Length[altList[[;;(%/. 0->l)]]]
t+altList[[t]]/200.
If[%>=60,s@Floor[%/60]~~" min ",""]~~s@Mod[%,60]~~ " sec"https://codegolf.stackexchange.com/questions/99949
复制相似问题