你在玩一个名叫1\text{D Array BattleGround}的著名游戏。在游戏中,玩家可以驻扎在从0到10^5的任何位置。
你是游戏中的伞兵,有能力做两种-手术。
每种类型的操作都需要1秒。
您驻扎在N,并希望在尽可能短的时间内前往M (1≤ N, M ≤10^4)。
找出到达M所需的最短时间。
注意:每次操作之后,您必须留在从0到10^5__的区域中。
Input : 4 6
Output: 2
Input : 10 1
Output: 9
Input : 1 3
Output: 3
Input : 2 10
Output: 5
Input : 666 6666
Output: 255
Input : 9999 10000
Output: 5000这是一个代码-高尔夫挑战,所以代码与最低字节赢!
发布于 2020-06-17 18:00:50
接受输入作为(N)(M)。
N=>g=M=>M>N?M%2-~g(M+1>>1):N-M我们不是从N到M,而是从M到N。
而M大于N:
当M小于或等于N时,剩下的操作数是N-M。
例如N=666__,M=6666__:
M | transformation | operations | total
------+--------------------+------------+-------
6666 | M / 2 = 3333 | 1 | 1
3333 | (M + 1) / 2 = 1667 | 2 | 3
1667 | (M + 1) / 2 = 834 | 2 | 5
834 | M / 2 = 417 | 1 | 6
417 | M + 249 = 666 | 249 | 255在反向操作的情况下,这会给出:
其背后的想法是,在流程开始时(即当N仍然很小的时候)处理最大数量的退步操作总是更便宜,而不是超出M太大,提前操作太仓促,然后再做退步。
发布于 2020-06-18 18:52:23
←V€⁰¡ṁ§eD←;在网上试试!参数的顺序是相反的(首先是目标,然后是初始位置)。
蛮力比更有效的方法更短。
←V€⁰¡ṁ§eD←; Inputs: M (stored in ⁰) and N (implicit).
; Wrap in list: [N]
¡ Iterate, returning an infinite list:
ṁ Map and concatenate:
← Decrement and
D double,
§e put the results in a two-element list.
V 1-based index of first list that
€⁰ Contains M.
← Decrement.发布于 2020-06-18 06:50:27
[ÐÆd#`DÉD½+;‚¼}ƾ+受@ArnauldJavaScript回答的启发,所以一定要投他一票!
[ # Start an infinite loop:
Ð # Triplicate the pair at the top
# (which will use the implicit input in the first iteration)
Æ # Reduce it by subtracting (N-M)
d # If this is non-negative (>=0):
# # Stop the infinite loop
` # Pop and push both values separated to the stack
D # Duplicate the top value `M`
É # Check if it's odd (1 if odd; 0 if even)
½ # If it's 1: increase the counter_variable by 1
D # (without popping by duplicating first)
+ # Add this 1/0 to `M`
; # And halve it
‚ # Then pair it back together with the `N`
¼ # At the end of each iteration, increase the counter_variable by 1
}Æ # After the infinite loop: reduce by subtracting again (N-M)
¾+ # And add the counter_variable to this
# (after which the result is output implicitly)https://codegolf.stackexchange.com/questions/206235
复制相似问题