首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >鸭鸭约瑟夫斯

鸭鸭约瑟夫斯
EN

Code Golf用户
提问于 2017-12-12 13:47:34
回答 22查看 5.1K关注 0票数 48

给定整数数组:

  1. 从第一个数字开始
  2. 向前跳n个位置,其中n是当前位置的值。
  3. 删除当前职位,使下一个职位成为当前职位。
  4. 步骤2,直到剩下一个数字为止
  5. 打印那个号码

规则

数组环绕(数组中最后一个数字之后的下一个数字是第一个数字)。

零自移(很明显)。

负数不允许作为输入。

测试用例

代码语言:javascript
复制
[1] => 1
[1,2] => 1
[1,2,3] => 3
[1,2,2] => 1
[1,2,3,4] => 1
[6,2,3,4] => 4
[1,2,3,4,5] => 5
[0,1] => 1
[0,0,2,0,0] => 0

逐步示例

代码语言:javascript
复制
[1,4,2,3,5]
 ^          start from the first position
   ^        jump 1 position (value of the position)
[1,  2,3,5] remove number in that position
     ^      take next position of the removed number (the 'new' 'current' position)
         ^  jump 2 positions
[1,  2,3  ] remove number in that position
 ^          take next position (looping on the end of the array)
     ^      jump 1 position
[1,    3  ] remove number in that position
       ^    take next position (looping)
 ^          jump 3 positions (looping on the end of the array)
[      3  ] remove number in that position
print 3

示例2

代码语言:javascript
复制
[4,3,2,1,6,3]
 ^            start from the first position
         ^    jump 4 positions
[4,3,2,1,  3] remove number in that position    
           ^  take next position
     ^        jump 3 positions
[4,3,  1,  3] remove number in that position    
       ^      take next position
           ^  jump 1 positions
[4,3,  1    ] remove number in that position    
 ^            take next position
   ^          jump 4 positions
[4,    1    ] remove number in that position    
       ^      take next position
 ^            jump 1 position
[      1    ] remove number in that position
print 1

这是密码-高尔夫,以字节为单位的最短答案获胜!

EN

回答 22

Code Golf用户

回答已采纳

发布于 2017-12-12 17:30:29

外壳,7字节

这将返回结果为单例列表。

代码语言:javascript
复制
ΩεSotṙ←

在网上试试!

解释

代码语言:javascript
复制
Ω               Until
 ε              the result is a singleton list
     ṙ          Rotate left by
  S   ←         the first element
   ot           Then remove the first element  
票数 11
EN

Code Golf用户

发布于 2017-12-12 19:43:50

红宝石,37字节

代码语言:javascript
复制
->r{r.rotate!(r[0]).shift while r[1]}

修改就地数组,这似乎是可以接受的输出.在网上试试!

票数 7
EN

Code Golf用户

发布于 2017-12-12 14:08:06

马蒂尔,21字节

代码语言:javascript
复制
1`yy)+ynX\[]w(5Mynq]x

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

解释

代码语言:javascript
复制
1        % Push 1: current position in the array
`        % Do...while
  yy     %   Duplicate top two elements in the stack. Takes input implicitly
         %   in the first iteration.
         %   STACK: array, position, array, position
  )      %   Get specified entry in the array
         %   STACK: array, position, selected entry
  +      %   Add
         %   STACK: array, position (updated)
  y      %   Duplicate from below
         %   STACK: array, position, array
  n      %   Number of elements of array
         %   STACK: array, position, number of elements or array
  X\     %   1-based modulus
         %   STACK: array, position (wrapped around)
  []     %   Push empty array
         %   STACK: array, position, []
  w      %   Swap
         %   STACK: array, [], position
  (      %   Write value into specified entry in array. Writing [] removes
         %   the entry
         %   STACK: array (with one entry removed)
  5M     %   Push latest used position. Because of the removal, this now
         %   points to the entry that was after the removed one
         %   STACK: array, position
  y      %   Duplicate from below
         %   STACK: array, position, array
  n      %   Number of elements of array
         %   STACK: array, position, number of elements of array
  q      %   Subtract 1
         %   STACK: array, position, number of elements of array minus 1
]        % End. If top of the stack is nonzero, proceed with next iteration
         % STACK: array (containing 1 entry), position
x        % Delete. Implicitly display
         % STACK: array (containing 1 entry)
票数 5
EN
页面原文内容由Code Golf提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

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

复制
相关文章

相似问题

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