Code Golf:锯齿形图案扫描

发布于 2024-09-05 12:14:14 字数 1415 浏览 7 评论 0原文

挑战

按字符数计算的最短代码,采用单个输入整数 N (N >= 3) 并返回一个索引数组,迭代时将遍历 Nx根据 JPEG“之字形”扫描模式的 N 矩阵。以下是遍历 8x8 矩阵的示例src

< img src="https://upload.wikimedia.org/wikipedia/commons/thumb/4/43/JPEG_ZigZag.svg/220px-JPEG_ZigZag.svg.png" alt="zigzag 布局模式">

示例

(中间矩阵是不是输入或输出的一部分,只是输入所代表的 NxN 矩阵的表示。)

                1 2 3
(Input) 3  -->  4 5 6  -->  1 2 4 7 5 3 6 8 9 (Output)
                7 8 9

                1  2  3  4
(Input) 4  -->  5  6  7  8  -->  1 2 5 9 6 3 4 7 10 13 14 11 8 12 15 16 (Output)
                9 10 11 12
               13 14 15 16

注意

  • 生成的数组的基数应该适合您的语言(例如,Matlab 数组是从 1 开始的,C++ 数组是从 0 开始的)。
  • 这与此问题相关。

额外奖励

扩展您的答案以获取两个输入 NM(N,M >=3),并在 Nx 上执行相同的扫描M 矩阵。 N 是列数,M 是行数。)

(在本例中,

                  1  2  3  4
(Input) 4 3  -->  5  6  7  8  -->  1 2 5 9 6 3 4 7 10 11 8 12 (Output)
                  9 10 11 12

                   1  2  3
(Input) 3 4  -->   4  5  6  -->  1 2 4 7 5 3 6 8 10 11 9 12 (Output)
                   7  8  9
                  10 11 12

The Challenge

The shortest code by character count that takes a single input integer N (N >= 3) and returns an array of indices that when iterated would traverse an NxN matrix according to the JPEG "zigzag" scan pattern. The following is an example traversal over an 8x8 matrixsrc:

zigzag layout pattern

Examples

(The middle matrix is not part of the input or output, just a representation of the NxN matrix the input represents.)

                1 2 3
(Input) 3  -->  4 5 6  -->  1 2 4 7 5 3 6 8 9 (Output)
                7 8 9

                1  2  3  4
(Input) 4  -->  5  6  7  8  -->  1 2 5 9 6 3 4 7 10 13 14 11 8 12 15 16 (Output)
                9 10 11 12
               13 14 15 16

Notes

  • The resulting array's base should be appropriate for your language (e.g., Matlab arrays are 1-based, C++ arrays are 0-based).
  • This is related to this question.

Bonus

Extend your answer to take two inputs N and M (N, M >=3) and perform the same scan over an NxM matrix. (In this case N would be the number of columns and M the number of rows.)

Bonus Examples

                  1  2  3  4
(Input) 4 3  -->  5  6  7  8  -->  1 2 5 9 6 3 4 7 10 11 8 12 (Output)
                  9 10 11 12

                   1  2  3
(Input) 3 4  -->   4  5  6  -->  1 2 4 7 5 3 6 8 10 11 9 12 (Output)
                   7  8  9
                  10 11 12

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

谈情不如逗狗 2024-09-12 12:14:14

J,13 15 个字符

;<@|.`</.i.2$

用法:

   ;<@|.`</.i.2$  3
0 1 3 6 4 2 5 7 8

   ;<@|.`</.i.2$  4
0 1 4 8 5 2 3 6 9 12 13 10 7 11 14 15

解释

NB. 是 J 的评论指示符)

;         NB. Link together...
<@|.`<    NB. ... 'take the reverse of' and 'take normally'
/.        NB. ... applied to alternating diagonals of...
i.        NB. ... successive integers starting at 0 and counting up to fill an array with dimensions of...
2$        NB. ... the input extended cyclically to a list of length two.

J,奖金,13 个字符

;<@|.`</.i.|.

用法:

   ;<@|.`</.i.|. 3 4
0 1 3 6 4 2 5 7 9 10 8 11

   ;<@|.`</.i.|. 9 6
0 1 9 18 10 2 3 11 19 27 36 28 20 12 4 5 13 21 29 37 45 46 38 30 22 14 6 7 15 23 31 39 47 48 40 32 24 16 8 17 25 33 41 49 50 42 34 26 35 43 51 52 44 53

J, 13 15 characters

;<@|.`</.i.2$

Usage:

   ;<@|.`</.i.2$  3
0 1 3 6 4 2 5 7 8

   ;<@|.`</.i.2$  4
0 1 4 8 5 2 3 6 9 12 13 10 7 11 14 15

Explanation

(NB. is J's comment indicator)

;         NB. Link together...
<@|.`<    NB. ... 'take the reverse of' and 'take normally'
/.        NB. ... applied to alternating diagonals of...
i.        NB. ... successive integers starting at 0 and counting up to fill an array with dimensions of...
2$        NB. ... the input extended cyclically to a list of length two.

J, bonus, 13 characters

;<@|.`</.i.|.

Usage:

   ;<@|.`</.i.|. 3 4
0 1 3 6 4 2 5 7 9 10 8 11

   ;<@|.`</.i.|. 9 6
0 1 9 18 10 2 3 11 19 27 36 28 20 12 4 5 13 21 29 37 45 46 38 30 22 14 6 7 15 23 31 39 47 48 40 32 24 16 8 17 25 33 41 49 50 42 34 26 35 43 51 52 44 53
断舍离 2024-09-12 12:14:14

Python, 92, 95, 110, 111, 114, 120, < s>122、162164 字符

N=input()
for a in sorted((p%N+p/N,(p%N,p/N)[(p%N-p/N)%2],p)for p in range(N*N)):print a[2],

测试:

$ echo 3 | python ./code-golf.py 
0 1 3 6 4 2 5 7 8

$ echo 4 | python ./code-golf.py 
0 1 4 8 5 2 3 6 9 12 13 10 7 11 14 15

此解决方案可以轻松推广到 NxM板:调整输入处理并将 N*N 替换为 N*M

N,M=map(int,raw_input().split())
for a in sorted((p%N+p/N,(p%N,p/N)[(p%N-p/N)%2],p)for p in range(N*M)):print a[2],

我怀疑有一些更简单/更短的方法来读取两个数字。

测试:

$ echo 4 3 | python ./code-golf.py 
0 1 4 8 5 2 3 6 9 10 7 11

Python, 92, 95, 110, 111, 114, 120, 122, 162, 164 chars

N=input()
for a in sorted((p%N+p/N,(p%N,p/N)[(p%N-p/N)%2],p)for p in range(N*N)):print a[2],

Testing:

$ echo 3 | python ./code-golf.py 
0 1 3 6 4 2 5 7 8

$ echo 4 | python ./code-golf.py 
0 1 4 8 5 2 3 6 9 12 13 10 7 11 14 15

This solution easily generalizes for NxM boards: tweak the input processing and replace N*N with N*M:

N,M=map(int,raw_input().split())
for a in sorted((p%N+p/N,(p%N,p/N)[(p%N-p/N)%2],p)for p in range(N*M)):print a[2],

I suspect there's some easier/shorter way to read two numbers.

Testing:

$ echo 4 3 | python ./code-golf.py 
0 1 4 8 5 2 3 6 9 10 7 11
挽你眉间 2024-09-12 12:14:14

Ruby, 69 89 字符

n=gets.to_i
puts (0...n*n).sort_by{|p|[t=p%n+p/n,[p%n,p/n][t%2]]}*' '

89 字符

n=gets.to_i
puts (0...n*n).map{|p|[t=p%n+p/n,[p%n,p/n][t%2],p]}.sort.map{|i|i[2]}.join' '

运行Credits to doublep。

> zigzag.rb
3
0 1 3 6 4 2 5 7 8

> zigzag.rb
4
0 1 4 8 5 2 3 6 9 12 13 10 7 11 14 15

为排序方法

Ruby, 69 89 chars

n=gets.to_i
puts (0...n*n).sort_by{|p|[t=p%n+p/n,[p%n,p/n][t%2]]}*' '

89 chars

n=gets.to_i
puts (0...n*n).map{|p|[t=p%n+p/n,[p%n,p/n][t%2],p]}.sort.map{|i|i[2]}.join' '

Run

> zigzag.rb
3
0 1 3 6 4 2 5 7 8

> zigzag.rb
4
0 1 4 8 5 2 3 6 9 12 13 10 7 11 14 15

Credits to doublep for the sort method.

超可爱的懒熊 2024-09-12 12:14:14

F#,126 个字符

let n=stdin.ReadLine()|>int
for i=0 to 2*n do for j in[id;List.rev].[i%2][0..i]do if i-j<n&&j<n then(i-j)*n+j|>printf"%d "

示例:

$ echo 3 | fsi --exec Program.fsx
0 1 3 6 4 2 5 7 8

$ echo 4 | fsi --exec Program.fsx
0 1 4 8 5 2 3 6 9 12 13 10 7 11 14 15

F#, 126 chars

let n=stdin.ReadLine()|>int
for i=0 to 2*n do for j in[id;List.rev].[i%2][0..i]do if i-j<n&&j<n then(i-j)*n+j|>printf"%d "

Examples:

$ echo 3 | fsi --exec Program.fsx
0 1 3 6 4 2 5 7 8

$ echo 4 | fsi --exec Program.fsx
0 1 4 8 5 2 3 6 9 12 13 10 7 11 14 15
等你爱我 2024-09-12 12:14:14

Golfscript,26/30 32/36 45 59 个字符

迄今为止最短的非 J 解决方案:

更新排序(不要告诉其他人! ) - 30 个字符:

 ~:1.*,{..1/\1%+.2%.+(@*]}

直接实现 - 36 个字符:

 ~:@.*,{[.@%:|\@/:^+|^- 2%^|if]}

如果您可以提供输出为“013642578”而不是“0 1 3 6 4 2 5 7 8”,那么您可以删除最后 4 个字符。

感谢 doublep 的排序技术。


说明:

~\:@*        #read input, store first number into @, multiply the two
,            #put range(@^2) on the stack
{...}$       #sort array using the key in ...
" "*         #join array w/ spaces

对于密钥:

[            #put into an array whatever is left on the stack until ]
.@%:|        #store @%n on the stack, also save it as |
\@/:^        #store @/n on the stack, also save it as ^
+            #add them together. this remains on the stack.
|^- 2%^|if   #if (| - ^) % 2 == 1, then put ^ on stack, else put | on stack.
]            #collect them into an array
'* #solution 1 #~:\.*,{.\/1$\%+.1&@.~if]}

直接实现 - 36 个字符:


如果您可以提供输出为“013642578”而不是“0 1 3 6 4 2 5 7 8”,那么您可以删除最后 4 个字符。

感谢 doublep 的排序技术。


说明:


对于密钥:


 '* #solution 2
#~\:1*,{..1/\1%+.2%.+(@*]}

直接实现 - 36 个字符:


如果您可以提供输出为“013642578”而不是“0 1 3 6 4 2 5 7 8”,那么您可以删除最后 4 个字符。

感谢 doublep 的排序技术。


说明:


对于密钥:


 '* #(bonus)
#~\:\*,{.\/1$\%+.1&@.~if]}

直接实现 - 36 个字符:


如果您可以提供输出为“013642578”而不是“0 1 3 6 4 2 5 7 8”,那么您可以删除最后 4 个字符。

感谢 doublep 的排序技术。


说明:


对于密钥:


 '* #(bonus)

直接实现 - 36 个字符:


如果您可以提供输出为“013642578”而不是“0 1 3 6 4 2 5 7 8”,那么您可以删除最后 4 个字符。

感谢 doublep 的排序技术。


说明:


对于密钥:


 '*
#~\:@*,{[.@%:|\@/:^+|^- 2%^|if]}

如果您可以提供输出为“013642578”而不是“0 1 3 6 4 2 5 7 8”,那么您可以删除最后 4 个字符。

感谢 doublep 的排序技术。


说明:


对于密钥:


 '* #solution 1
#~:\.*,{.\/1$\%+.1&@.~if]}

直接实现 - 36 个字符:


如果您可以提供输出为“013642578”而不是“0 1 3 6 4 2 5 7 8”,那么您可以删除最后 4 个字符。

感谢 doublep 的排序技术。


说明:


对于密钥:


 '* #solution 2
#~\:1*,{..1/\1%+.2%.+(@*]}

直接实现 - 36 个字符:


如果您可以提供输出为“013642578”而不是“0 1 3 6 4 2 5 7 8”,那么您可以删除最后 4 个字符。

感谢 doublep 的排序技术。


说明:


对于密钥:


 '* #(bonus)
#~\:\*,{.\/1$\%+.1&@.~if]}

直接实现 - 36 个字符:


如果您可以提供输出为“013642578”而不是“0 1 3 6 4 2 5 7 8”,那么您可以删除最后 4 个字符。

感谢 doublep 的排序技术。


说明:


对于密钥:


 '* #(bonus)

直接实现 - 36 个字符:

如果您可以提供输出为“013642578”而不是“0 1 3 6 4 2 5 7 8”,那么您可以删除最后 4 个字符。

感谢 doublep 的排序技术。


说明:

对于密钥:

'* #(bonus)

如果您可以提供输出为“013642578”而不是“0 1 3 6 4 2 5 7 8”,那么您可以删除最后 4 个字符。

感谢 doublep 的排序技术。


说明:

对于密钥:

'* #solution 1 #~:\.*,{.\/1$\%+.1&@.~if]}

直接实现 - 36 个字符:

如果您可以提供输出为“013642578”而不是“0 1 3 6 4 2 5 7 8”,那么您可以删除最后 4 个字符。

感谢 doublep 的排序技术。


说明:

对于密钥:

'* #solution 2 #~\:1*,{..1/\1%+.2%.+(@*]}

直接实现 - 36 个字符:

如果您可以提供输出为“013642578”而不是“0 1 3 6 4 2 5 7 8”,那么您可以删除最后 4 个字符。

感谢 doublep 的排序技术。


说明:

对于密钥:

'* #(bonus) #~\:\*,{.\/1$\%+.1&@.~if]}

直接实现 - 36 个字符:

如果您可以提供输出为“013642578”而不是“0 1 3 6 4 2 5 7 8”,那么您可以删除最后 4 个字符。

感谢 doublep 的排序技术。


说明:

对于密钥:

'* #(bonus)

直接实现 - 36 个字符:

如果您可以提供输出为“013642578”而不是“0 1 3 6 4 2 5 7 8”,那么您可以删除最后 4 个字符。

感谢 doublep 的排序技术。


说明:

对于密钥:

Golfscript, 26/30 32/36 45 59 characters

Shortest non-J solution so far:

Updated sort (don't tell the others!) - 30 chars:

 ~:1.*,{..1/\1%+.2%.+(@*]}

Straight implementation - 36 chars:

 ~:@.*,{[.@%:|\@/:^+|^- 2%^|if]}

If you can provide output as "013642578" instead of "0 1 3 6 4 2 5 7 8", then you can remove the last 4 characters.

Credit to doublep for the sorting technique.


Explanation:

~\:@*        #read input, store first number into @, multiply the two
,            #put range(@^2) on the stack
{...}$       #sort array using the key in ...
" "*         #join array w/ spaces

and for the key:

[            #put into an array whatever is left on the stack until ]
.@%:|        #store @%n on the stack, also save it as |
\@/:^        #store @/n on the stack, also save it as ^
+            #add them together. this remains on the stack.
|^- 2%^|if   #if (| - ^) % 2 == 1, then put ^ on stack, else put | on stack.
]            #collect them into an array
'* #solution 1 #~:\.*,{.\/1$\%+.1&@.~if]}

Straight implementation - 36 chars:


If you can provide output as "013642578" instead of "0 1 3 6 4 2 5 7 8", then you can remove the last 4 characters.

Credit to doublep for the sorting technique.


Explanation:


and for the key:


 '* #solution 2
#~\:1*,{..1/\1%+.2%.+(@*]}

Straight implementation - 36 chars:


If you can provide output as "013642578" instead of "0 1 3 6 4 2 5 7 8", then you can remove the last 4 characters.

Credit to doublep for the sorting technique.


Explanation:


and for the key:


 '* #(bonus)
#~\:\*,{.\/1$\%+.1&@.~if]}

Straight implementation - 36 chars:


If you can provide output as "013642578" instead of "0 1 3 6 4 2 5 7 8", then you can remove the last 4 characters.

Credit to doublep for the sorting technique.


Explanation:


and for the key:


 '* #(bonus)

Straight implementation - 36 chars:


If you can provide output as "013642578" instead of "0 1 3 6 4 2 5 7 8", then you can remove the last 4 characters.

Credit to doublep for the sorting technique.


Explanation:


and for the key:


 '*
#~\:@*,{[.@%:|\@/:^+|^- 2%^|if]}

If you can provide output as "013642578" instead of "0 1 3 6 4 2 5 7 8", then you can remove the last 4 characters.

Credit to doublep for the sorting technique.


Explanation:


and for the key:


 '* #solution 1
#~:\.*,{.\/1$\%+.1&@.~if]}

Straight implementation - 36 chars:


If you can provide output as "013642578" instead of "0 1 3 6 4 2 5 7 8", then you can remove the last 4 characters.

Credit to doublep for the sorting technique.


Explanation:


and for the key:


 '* #solution 2
#~\:1*,{..1/\1%+.2%.+(@*]}

Straight implementation - 36 chars:


If you can provide output as "013642578" instead of "0 1 3 6 4 2 5 7 8", then you can remove the last 4 characters.

Credit to doublep for the sorting technique.


Explanation:


and for the key:


 '* #(bonus)
#~\:\*,{.\/1$\%+.1&@.~if]}

Straight implementation - 36 chars:


If you can provide output as "013642578" instead of "0 1 3 6 4 2 5 7 8", then you can remove the last 4 characters.

Credit to doublep for the sorting technique.


Explanation:


and for the key:


 '* #(bonus)

Straight implementation - 36 chars:

If you can provide output as "013642578" instead of "0 1 3 6 4 2 5 7 8", then you can remove the last 4 characters.

Credit to doublep for the sorting technique.


Explanation:

and for the key:

'* #(bonus)

If you can provide output as "013642578" instead of "0 1 3 6 4 2 5 7 8", then you can remove the last 4 characters.

Credit to doublep for the sorting technique.


Explanation:

and for the key:

'* #solution 1 #~:\.*,{.\/1$\%+.1&@.~if]}

Straight implementation - 36 chars:

If you can provide output as "013642578" instead of "0 1 3 6 4 2 5 7 8", then you can remove the last 4 characters.

Credit to doublep for the sorting technique.


Explanation:

and for the key:

'* #solution 2 #~\:1*,{..1/\1%+.2%.+(@*]}

Straight implementation - 36 chars:

If you can provide output as "013642578" instead of "0 1 3 6 4 2 5 7 8", then you can remove the last 4 characters.

Credit to doublep for the sorting technique.


Explanation:

and for the key:

'* #(bonus) #~\:\*,{.\/1$\%+.1&@.~if]}

Straight implementation - 36 chars:

If you can provide output as "013642578" instead of "0 1 3 6 4 2 5 7 8", then you can remove the last 4 characters.

Credit to doublep for the sorting technique.


Explanation:

and for the key:

'* #(bonus)

Straight implementation - 36 chars:

If you can provide output as "013642578" instead of "0 1 3 6 4 2 5 7 8", then you can remove the last 4 characters.

Credit to doublep for the sorting technique.


Explanation:

and for the key:

泪之魂 2024-09-12 12:14:14

MATLAB,101/116 个字符

它基本上是此处给出的相同答案的压缩版本,直接在命令提示符下运行:

N=input('');i=fliplr(spdiags(fliplr(reshape(1:N*N,N,N)')));i(:,1:2:end)=flipud(i(:,1:2:end));i(i~=0)'

以及从用户读取两个值的扩展:

S=str2num(input('','s'));i=fliplr(spdiags(fliplr(reshape(1:prod(S),S)')));i(:,1:2:end)=flipud(i(:,1:2:end));i(i~=0)'

测试:

3
ans =
     1     2     4     7     5     3     6     8     9

4 3
ans =
     1     2     5     9     6     3     4     7    10    11     8    12

MATLAB, 101/116 chars

Its basically a condensed version of the same answer given here, to be run directly on the command prompt:

N=input('');i=fliplr(spdiags(fliplr(reshape(1:N*N,N,N)')));i(:,1:2:end)=flipud(i(:,1:2:end));i(i~=0)'

and an extended one that read two values from the user:

S=str2num(input('','s'));i=fliplr(spdiags(fliplr(reshape(1:prod(S),S)')));i(:,1:2:end)=flipud(i(:,1:2:end));i(i~=0)'

Testing:

3
ans =
     1     2     4     7     5     3     6     8     9

and

4 3
ans =
     1     2     5     9     6     3     4     7    10    11     8    12
故事还在继续 2024-09-12 12:14:14

Ruby 137 130 138 个字符

n=gets.to_i
def g(a,b,r,t,s);x=[s*r]*t;t==r ?[a,x,a]:[a,x,g(b,a,r,t+1,-s),x,a];end
q=0;puts ([1]+g(1,n,n-1,1,1)).flatten.map{|s|q+=s}*' '

$ zz.rb
3
1 2 4 7 5 3 6 8 9

$ zz.rb
4
1 2 5 9 6 3 4 7 10 13 14 11 8 12 15 16

Ruby 137 130 138 characters

n=gets.to_i
def g(a,b,r,t,s);x=[s*r]*t;t==r ?[a,x,a]:[a,x,g(b,a,r,t+1,-s),x,a];end
q=0;puts ([1]+g(1,n,n-1,1,1)).flatten.map{|s|q+=s}*' '

$ zz.rb
3
1 2 4 7 5 3 6 8 9

$ zz.rb
4
1 2 5 9 6 3 4 7 10 13 14 11 8 12 15 16
漫漫岁月 2024-09-12 12:14:14

C89(280字节)

我想这仍然可以优化 - 我使用四个数组来存储可能的移动
撞墙时的矢量。我想这是可以做到的,在定义处节省一些字符,但我认为进一步实现逻辑会花费更多。不管怎样,就这样吧:

t,l,b,r,i,v,n;main(int c,char**a){n=atoi(*++a);b=n%2;int T[]={n-1,1},L[]={1-n,n}
,B[]={1-n,1},R[]={n-1,n};for(c=n*n;c--;){printf("%d%c",i,c?32:10);if(i>=n*(n-1))
v=B[b=!b];else if(i%n>n-2){if(!(n%2)&&i<n)goto g;v=R[r=!r];}else if(i<n)g:v=T[t=
!t];else if(!(i%n))v=L[l=!l];i+=v;}}

编译时有一些警告,但据我所知它是可移植的 C89。我其实不确定
不管我的算法是否聪明,也许你可以用更好的算法来缩短时间
(还没有花时间了解其他解决方案)。

C89 (280 bytes)

I guess this can still be optimized - I use four arrays to store the possible movement
vectors when hitting a wall. I guess it can be done, saving some chars at the definition, but I think it will cost more to implement the logic further down. Anyway, here you go:

t,l,b,r,i,v,n;main(int c,char**a){n=atoi(*++a);b=n%2;int T[]={n-1,1},L[]={1-n,n}
,B[]={1-n,1},R[]={n-1,n};for(c=n*n;c--;){printf("%d%c",i,c?32:10);if(i>=n*(n-1))
v=B[b=!b];else if(i%n>n-2){if(!(n%2)&&i<n)goto g;v=R[r=!r];}else if(i<n)g:v=T[t=
!t];else if(!(i%n))v=L[l=!l];i+=v;}}

compiles with a few warnings, but as far as I know it is portable C89. I'm actually not sure
whether my algorithm is clever at all, maybe you can get way shorter with a better one
(haven't taken the time to understand the other solutions yet).

薄荷港 2024-09-12 12:14:14

Haskell 117 个字符

i s=concatMap(\q->d q++(reverse.d$q+1))[0,2..s+s]
 where d r=[x+s*(r-x)|x<-[0..r],x<s&&(r-x)<s]
main=readLn>>=print.i

运行:

$ echo 3 | ./Diagonals 
[0,1,3,6,4,2,5,7,8]

$ echo 4 | ./Diagonals 
[0,1,4,8,5,2,3,6,9,12,13,10,7,11,14,15]

矩形变体稍长,为 120 个字符:

j(w,h)=concatMap(\q->d q++(reverse.d$q+1))[0,2..w+h]
 where d r=[x+w*(r-x)|x<-[0..r],x<w&&(r-x)<h]
main=readLn>>=print.j

此处输入需要一个元组:

$ echo '(4,3)' | ./Diagonals 
[0,1,4,8,5,2,3,6,9,10,7,11]

$ echo '(3,4)' | ./Diagonals 
[0,1,3,6,4,2,5,7,9,10,8,11]

答案全部基于 0,并以列表形式返回(Haskell 的自然形式)。

Haskell 117 characters

i s=concatMap(\q->d q++(reverse.d$q+1))[0,2..s+s]
 where d r=[x+s*(r-x)|x<-[0..r],x<s&&(r-x)<s]
main=readLn>>=print.i

Runs:

$ echo 3 | ./Diagonals 
[0,1,3,6,4,2,5,7,8]

$ echo 4 | ./Diagonals 
[0,1,4,8,5,2,3,6,9,12,13,10,7,11,14,15]

The rectangular variant is a little longer, at 120 characters:

j(w,h)=concatMap(\q->d q++(reverse.d$q+1))[0,2..w+h]
 where d r=[x+w*(r-x)|x<-[0..r],x<w&&(r-x)<h]
main=readLn>>=print.j

Input here requires a tuple:

$ echo '(4,3)' | ./Diagonals 
[0,1,4,8,5,2,3,6,9,10,7,11]

$ echo '(3,4)' | ./Diagonals 
[0,1,3,6,4,2,5,7,9,10,8,11]

Answers are all 0-based, and returned as lists (natural forms for Haskell).

秋叶绚丽 2024-09-12 12:14:14

Perl 102 字符

<>=~/ /;print map{$_%1e6," "}sort map{($x=($%=$_%

Perl 102 字符

)+($/=int$_/

Perl 102 字符

))*1e9+($x%2?$/:$%)*1e6+$_}0..

用法:

echo 3 4 | perl zigzag.pl
0 1 3 6 4 2 5 7 9 10 8 11
*

Perl 102 字符

-1

用法:

Perl 102 characters

<>=~/ /;print map{$_%1e6," "}sort map{($x=($%=$_%

Perl 102 characters

)+($/=int$_/

Perl 102 characters

))*1e9+($x%2?$/:$%)*1e6+$_}0..

usage :

echo 3 4 | perl zigzag.pl
0 1 3 6 4 2 5 7 9 10 8 11
*

Perl 102 characters

-1

usage :

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文