Answers:
l~/{_1fb_,Y${\(_@=\}%:++\z}2*;=
像输入
4 [1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16]
1如果是幻方,0则输出,否则输出。
运作方式:
l~/ "Evaluates the input and split the array into chunks"
"of size N where N is the first integer";
{ }2* "Run this code block 2 times";
_1fb "Copy the 2D array and calculate sum of each row of copy";
_, "Copy the array containing sum of each row and get"
"its length. This is equal to N";
Y${ }% "Run this code block for each array of the original"
"2D array that we copied from stack";
\(_ "Put the length number to top of stack, decrement and"
"copy that";
@=\ "Take the element at that index from each row and put"
"N back behind at second position in stack";
:+ "Take sum of elements of the array. This is sum of"
"one of the diagonals of the 2D array";
+ "Push diagonal sum to row sum array";
\z "Bring original array to top and transpose columns";
; "At this point, the stack contain 3 arrays:"
" Array with sum of rows and main diagonal,"
" Array with sum of columns and secondary diagonal and"
" The original array. Pop the original array";
= "Check if sum of rows + main diagonal array is equal to ";
"sum of columns + secondary diagonal array";
这可以打得更远。
n,l=input()
r=range
print r(1,n*n+1)==sorted(l)*len({sum(l[i::j][:n])for(i,j)in zip(r(n)+r(0,n*n,n)+[0,n-1],[n]*n+[1]*n+[n+1,n-1])})
运行示例:
STDIN: 4,[16,3,2,13,5,10,11,8,9,6,7,12,4,15,14,1]
Output: True
有两件事要检查:
[1,2,...,n*n].通过获取对应于这些子集的切片的总和来检查第一个。每行,每列或每条对角线均以其起始值和位移来描述。我们获取列表中对应的切片,将其截断n并求和。用Python的[start:end:step]表示法,行是[r*n::1],列是[c::n],两个对角线是[0::n+1]和[n-1::n-1]。这些存储2*n+2为由产生的对的列表zip。
我们取总和集并检查它的长度是否为1。此外,我们对输入进行排序并检查它是否为列表。[1,2,...,n*n].实际上,我们乘以sorted(l)总和的长度将两者合并为一个校验,始终除非总和的长度为1,否则失败。
d = Diagonal; r = Reverse; i = Input[];
Length@Union[Tr /@ Join[p = Partition[i[[2]], i[[1]]],
t = Transpose@p, {d@p}, {d@t}, {d@r@p}, {d@r@t}]] == 1
接受诸如
{4,{16, 3, 2, 13, 5, 10, 11, 8, 9, 6, 7, 12, 4, 15, 14, 1}}
真正
Input[r=Reverse]保存一个字节。#&@@比[[1]]。短一个字节。您可能还可以在Partition另一个字节中使用中缀表示法。并且Thread应该代替Transpose。或者,将此Unicode字符用作后缀运算符(Mathematica将其用于上标T进行转置)。
杀伤人员地雷47 32
使用TwiNight的出色解决方案并进行更多调整:
∧/2=/+⌿(1 1∘⍉∘⌽,1 1∘⍉,⍉,⊢)⎕⍴⍨,⍨⎕
说明:
这使用功能列,该功能列在Dyalog解释器的v14中引入。APL从右到左执行,,是输入,因此首先是维,然后是数字向量。
⍨⎕,⍨⎕创建矩阵NxN
之后是函数系列,基本上只是应用于正确参数的一系列函数(在方括号之间)。这些功能是:
⊢返回恰到好处的参数(即矩阵)
s转置正确的参数矩阵
11∘⍉返回对角线
11∘⍉∘⌽返回反转(水平)矩阵的对角线
所有结果都与函数“,”连接
此时,结果是一个矩阵,然后将其列求和(+ sum)。然后将以此方式获得的值与∧/ 2 = /检查是否相同。
我也将旧解决方案留在这里:
{M←⍺ ⍺⍴⍵⋄d←M=⍉M⋄(⊃≡∪)((+/,+⌿)M),+/∘,¨d(⌽d)ר⊂M}
将维作为左参数,将元素向量作为右参数,例如:
4{M←⍺ ⍺⍴⍵⋄d←M=⍉M⋄(⊃≡∪)((+/,+⌿)M),+/∘,¨d(⌽d)ר⊂M}16 3 2 13 5 10 11 8 9 6 7 12 4 15 14 1
1
可以在这里在线尝试:www.tryapl.org
~]:q(/q(/zip+[q()/{(\;}%]+[q((/);(;{(\;}%]+{{+}*}%.&,2<q(2?,{)}%-!*
使用提示符读取输入并显示输出。
在控制台中使用FireFox> 31进行测试(Array.fill是非常新的)
z=(p=prompt)(n=p()|0).split(' '),u=Array(2*n).fill(e=d=n*(n*n+1)/2),z.map((v,i)=>(r=i/n|0,u[r+n]-=v,u[c=i%n]-=v,d-=v*(r==c),e-=v*(r+c+1==n))),o=!(e|d|u.some(v=>v)),z.sort((a,b)=>a-b||(o=0)),p(o)
少打高尔夫球
n = prompt()|0; // input side length
z = prompt().split(' '); // input list of space separeted numbers
e = d = n*(n*n+1)/2; // Calc sum for each row, column and diagonal
u = Array(2*n).fill(e), // Init check values for n rows and n columns
z.map( (v,i) => { // loop on number array
r = i / n | 0; // row number
c = i % n; // column number
u[r+n] -= v; // subtract current value, if correct it will be 0 at loop end
u[c] -= v;
if (r==c) d -= v; // subtract if diagonal \
if (r+c+1==n) e -=v; // subtract if diagonal /
}),
o=!(e|d|u.some(v=>v)); // true if values for rows, cols and diags are 0
z.sort((a,b)=>a-b||(o=0)); // use sort to verify if there are repeated values in input
alert(o);
&q1l{sM++JcEQCJm.e@bkd_BJqSlQS
在这里在线尝试。
&q1l{sM++JcEQCJm.e@bkd_BJqSlQSQ Implicit: Q = evaluated 1st input (contents), E = evaluated 2nd input (side length)
Trailing Q inferred
cEQ Chop E into pieces or length Q
J Store in J
_BJ Pair J with itself with rows reversed
m Map the original and it's reverse, as d, using:
.e d Map each row in d, as b with index k, using:
@bk Get the kth element of b
The result of this map is [[main diagonal], [antidiagonal]]
+J Prepend rows from J
+ CJ Prepend columns from J (transposed J)
sM Sum each
{ Deduplicate
l Length
q1 Is the above equal to 1?
& Logic AND the above with...
SlQ ... is the range [1-length(Q)]...
q ... equal to...
SQ ... sorted(Q)
编辑:修复了一个错误,感谢@KevinCruijssen让我知道:o)
True数字太大或不是唯一的幻方。即4和[12,26,23,13,21,15,18,20,17,19,22,16,24,14,11,25]或4和[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]同时输出True。(尽管几乎所有现有的答案都存在相同的问题,但是由于它们是4年前发布的,因此我不愿在评论中更正他们的错误。)
ô©O®øO®Å\O®Å/O)˜Ë²{¹nLQ*
输入格式:4\n[2,16,13,3,11,5,8,10,7,9,12,6,14,4,1,15]。输出1/ 0对truthy / falsey分别。
说明:
ô # Split the 2nd (implicit) input into parts of a size of the 1st (implicit) input
# i.e. [2,16,13,3,11,5,8,10,7,9,12,6,14,4,1,15] and 4
# → [[2,16,13,3],[11,5,8,10],[7,9,12,6],[14,4,1,15]]
© # Store it in the register (without popping)
O # Take the sum of each row
# i.e. [[2,16,13,3],[11,5,8,10],[7,9,12,6],[14,4,1,15]] → [34,34,34,34]
® # Push the matrix from the register again
ø # Zip/transpose; swapping rows/columns
# i.e. [[2,16,13,3],[11,5,8,10],[7,9,12,6],[14,4,1,15]]
# → [[2,11,7,14],[16,5,9,4],[13,8,12,1],[3,10,6,15]]
O # Sum each inner list again
# i.e. [[2,11,7,14],[16,5,9,4],[13,8,12,1],[3,10,6,15]] → [34,34,34,34]
® # Push the matrix from the register again
Å\ # Get the top-left to bottom-right main diagonal of it
# i.e. [[2,16,13,3],[11,5,8,10],[7,9,12,6],[14,4,1,15]] → [2,5,12,15]
O # Sum it together
# i.e. [2,5,12,15] → 34
® # Push the matrix from the register again
Å/ # Get the top-right to bottom-left main diagonal of it
# i.e. [[2,16,13,3],[11,5,8,10],[7,9,12,6],[14,4,1,15]] → [3,8,9,14]
O # Sum it together
# i.e. [3,8,9,14] → 34
) # Wrap everything on the stack into a list
# → [[34,34,34,34],[34,34,34,34],34,34]
˜ # Flatten this list
# i.e. [[34,34,34,34],[34,34,34,34],34,34] → [34,34,34,34,34,34,34,34,34,34]
Ë # Check if all values are equal to each other
# i.e. [34,34,34,34,34,34,34,34,34,34] → 1 (truthy)
² # Push the second input again
{ # Sort it
# i.e. [2,16,13,3,11,5,8,10,7,9,12,6,14,4,1,15]
# → [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]
¹n # Push the first input again, and take its square
# i.e. 4 → 16
L # Create a list in the range [1, squared_input]
# i.e. 16 → [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]
Q # Check if the two lists are equal
# i.e. [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]
# and [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16] → 1 (truthy)
* # Check if both checks are truthy by multiplying them with each other
# i.e. 1 and 1 → 1
# (and output the result implicitly)
(i,j)更有效地为单号x,以i=x%C和j=x/C为一些足够大的C。可能以后再试一下。