首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >去禁区!

去禁区!
EN

Code Golf用户
提问于 2020-06-17 17:27:08
回答 9查看 1.6K关注 0票数 20

你在玩一个名叫1\text{D Array BattleGround}的著名游戏。在游戏中,玩家可以驻扎在从010^5的任何位置。

你是游戏中的伞兵,有能力做两种-手术。

  • 前进,这将使你的位置乘以2
  • 后退,这将通过1降低当前位置。

每种类型的操作都需要1秒。

您驻扎在N,并希望在尽可能短的时间内前往M (1≤ N, M ≤10^4)。

找出到达M所需的最短时间。

注意:每次操作之后,您必须留在从010^5__的区域中。

样本

代码语言:javascript
复制
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

这是一个代码-高尔夫挑战,所以代码与最低字节赢!

EN

回答 9

Code Golf用户

发布于 2020-06-17 18:00:50

JavaScript (ES6),30字节

接受输入作为(N)(M)

代码语言:javascript
复制
N=>g=M=>M>N?M%2-~g(M+1>>1):N-M

在网上试试!

怎么做?

我们不是从NM,而是从MN

M大于N

  • 如果M是奇数,增加它并除以2 (2个操作)
  • 如果M是偶数,只需除以2 (1操作)即可。

M小于或等于N时,剩下的操作数是N-M

例如N=666__,M=6666__:

代码语言:javascript
复制
  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

在反向操作的情况下,这会给出:

((((666-249)\times 2)\times 2-1)\times 2-1)\times 2=6666

其背后的想法是,在流程开始时(即当N仍然很小的时候)处理最大数量的退步操作总是更便宜,而不是超出M太大,提前操作太仓促,然后再做退步。

票数 23
EN

Code Golf用户

发布于 2020-06-18 18:52:23

外壳,11字节

代码语言:javascript
复制
←V€⁰¡ṁ§eD←;

在网上试试!参数的顺序是相反的(首先是目标,然后是初始位置)。

解释

蛮力比更有效的方法更短。

代码语言:javascript
复制
←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.
票数 6
EN

Code Golf用户

发布于 2020-06-18 06:50:27

05AB1E,18 字节数

代码语言:javascript
复制
[ÐÆd#`DÉD½+;‚¼}ƾ+

@ArnauldJavaScript回答的启发,所以一定要投他一票!

在网上试试验证所有测试用例.

解释:

代码语言:javascript
复制
[         # 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)
票数 3
EN
页面原文内容由Code Golf提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://codegolf.stackexchange.com/questions/206235

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档