给定整数数组:
数组环绕(数组中最后一个数字之后的下一个数字是第一个数字)。
零自移(很明显)。
负数不允许作为输入。
[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[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[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这是密码-高尔夫,以字节为单位的最短答案获胜!
发布于 2017-12-12 17:30:29
发布于 2017-12-12 19:43:50
发布于 2017-12-12 14:08:06
1`yy)+ynX\[]w(5Mynq]x1 % 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)https://codegolf.stackexchange.com/questions/150447
复制相似问题