ตรวจสอบ Magic Square


10

ตารางมายากลเป็นอาร์เรย์ตารางของตัวเลขที่มีด้าน n ประกอบด้วยจำนวนเต็มบวกชัดเจน 1, 2, ... , n ²จัดดังกล่าวว่าผลรวมของnตัวเลขในแนวนอนแนวตั้งหรือแนวทแยงสายหลัก ๆ อยู่เสมอ หมายเลขเดียวกันซึ่งเรียกว่าค่าคงที่วิเศษ

โปรแกรมของคุณจะต้องใส่ตัวเลขที่ระบุความยาวด้านข้างของรูปสี่เหลี่ยมจัตุรัสของคุณจากนั้นเป็นตัวเลขในช่องสี่เหลี่ยม ไม่สามารถใช้ตัวเลขได้มากกว่าหนึ่งครั้งไม่สามารถใช้ตัวเลขที่มากกว่าn ²และตัวเลขทั้งหมดต้องมากกว่า 0 โปรแกรมต้องพิจารณาว่าการรวมกันของตัวเลขนั้นเป็นตารางเวทย์มนตร์หรือไม่

คำตอบ:


4

CJam, 47 39 35 33 31 ไบต์

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";

นี้สามารถเล่นกอล์ฟต่อไป

ลองออนไลน์ได้ที่นี่


6

Python 2: 132 ตัวอักษร

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. องค์ประกอบมีการเปลี่ยนแปลงของ [1,2,...,n*n].

สิ่งแรกคือการตรวจสอบโดยใช้จำนวนเงินชิ้นที่สอดคล้องกับส่วนย่อยเหล่านี้ แต่ละแถวคอลัมน์หรือเส้นทแยงมุมอธิบายโดยค่าเริ่มต้นและการกระจัดของมัน เรานำรายการที่สอดคล้องกันตัดทอนไปยังnองค์ประกอบและรวม ใน ธ[start:end:step]โน้ต, แถว[r*n::1], คอลัมน์[c::n]และทั้งสองเส้นทแยงมุมเป็นและ[0::n+1] [n-1::n-1]เหล่านี้จะถูกเก็บไว้เป็นรายการคู่ที่ผลิตโดย2*n+2zip

เราใช้จำนวนผลรวมและตรวจสอบว่ามันมีความยาว 1 นอกจากนี้เราเรียงลำดับอินพุตและตรวจสอบว่าเป็นรายการที่[1,2,...,n*n].จริงแล้วเรารวมทั้งสองเป็นหนึ่งเช็คโดยการคูณsorted(l)ด้วยความยาวของผลรวมตรวจสอบที่เสมอ ล้มเหลวเว้นแต่ชุดผลรวมมีความยาว 1


ฉันรู้ว่าคุณสามารถเข้ารหัสคู่(i,j)ได้อย่างมีประสิทธิภาพมากขึ้นเป็นหมายเลขเดียวxซักi=x%Cและสำหรับบางคนมากพอj=x/C Cอาจให้ภาพในภายหลัง
xnor

5

APL, 35

∧/2=/(+⌿x,⍉x),+/↑1 1∘⍉¨x(⌽x←⎕⍴⍨,⍨⎕)

คำอธิบาย
x←⎕⍴⍨,⍨⎕แจ้งให้ใส่รูปร่างลงในเมทริกซ์และกำหนดให้x
กลับเมทริกซ์จากซ้ายไปขวา
x(...)สร้างอาร์เรย์ของเมทริกซ์: xและxย้อนกลับ
1 1∘⍉¨สำหรับเมทริกซ์แต่ละตัวนั้นให้ใช้
+/↑รูปแบบทแยงมุมเมทริกซ์ 2x ×ของตัวเลขบน เส้นทแยงมุมและรวมแถว

⍉xแปลงx
x,จากนั้นเชื่อมต่อด้วยxเพื่อสร้างเมทริกซ์× 2n
+⌿และรวมคอลัมน์

(+⌿x,⍉x),+/↑1 1∘⍉¨x(⌽x←⎕⍴⍨,⍨⎕)เชื่อมต่อกับรูปแบบของผลรวม
2=/ตรวจสอบว่าคู่ติดต่อกันเท่ากัน
∧/และและร่วมกันผลลัพธ์เหล่านั้นทั้งหมด


3

Mathematica 128 125

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}}

จริง


แน่นอนมีช่องว่างมากมายที่สามารถลบได้ที่นี่
Beta Decay

พื้นที่สีขาวทั้งหมดสามารถลบออกได้ ฉันทิ้งไว้ที่นั่นเพื่ออ่าน และฉันไม่ได้นับพื้นที่สีขาวที่ไม่จำเป็น
DavidC

คุณสามารถทำได้Input[r=Reverse]เพื่อบันทึกไบต์ เป็นไบต์สั้นกว่า#&@@ [[1]]คุณสามารถใช้สัญลักษณ์เสริมPartitionสำหรับไบต์อื่นได้ และควรจะทำงานแทนThread Transposeหรือใช้อักขระ Unicode นี้เป็นตัวดำเนินการแก้ไขปัญหาหลัง (Mathematica ใช้สำหรับตัวยก T สำหรับการแปลง)
Martin Ender

3

APL 47 32

ใช้โซลูชันที่ยอดเยี่ยมของ TwiNight และใช้ tweaking เพิ่มเติม:

∧/2=/+⌿(1 1∘⍉∘⌽,1 1∘⍉,⍉,⊢)⎕⍴⍨,⍨⎕

คำอธิบาย:

วิธีนี้ใช้รถไฟฟังก์ชั่นซึ่งเปิดตัวใน v14 ของล่าม Dyalog APL ถูกดำเนินการจากขวาไปซ้าย, คืออินพุตดังนั้นขนาดแรกจากนั้นเวกเตอร์ของตัวเลข

⎕⍴⍨, ⍨⎕สร้างเมทริกซ์ NxN

หลังจากนั้นฟังก์ชั่นรถไฟซึ่งโดยทั่วไปเป็นเพียงลำดับของฟังก์ชั่น (ระหว่างวงเล็บ) นำไปใช้กับอาร์กิวเมนต์ที่ถูกต้อง ฟังก์ชั่นคือ:

⊢ส่งคืนอาร์กิวเมนต์ที่ถูกต้อง (นั่นคือเมทริกซ์)

⍉แปลงเมทริกซ์อาร์กิวเมนต์ที่ถูกต้อง

1 1∘⍉คืนค่าเส้นทแยงมุม

1 1∘⍉∘⌽ส่งคืนเส้นทแยงมุมของเมทริกซ์ที่กลับด้าน (แนวนอน)

ผลลัพธ์ทั้งหมดจะถูกรวมเข้ากับฟังก์ชัน ","

ณ จุดนี้ผลลัพธ์คือเมทริกซ์ที่มีการรวมคอลัมน์แล้ว (+ ⌿) ค่าที่ได้จากวิธีนี้จะถูกตรวจสอบแล้วว่าเหมือนกันกับ with / 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



2

JavaScript (E6) 194

การใช้พรอมต์เพื่ออ่านอินพุตและจอแสดงผล
ทดสอบในคอนโซลด้วย 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)

น้อย golfed

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);

2

Pyth, 24 30 ไบต์

&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 ปีที่แล้วฉันไม่ได้ใส่ใจที่จะแก้ไขข้อผิดพลาดในความคิดเห็น)
Kevin Cruijssen

@KevinCruijssen ประณามฉันมุ่งเน้นไปที่การตรวจสอบผลรวมที่ฉันลืมเกี่ยวกับข้อกำหนดอื่น ๆ ... ฉันช่างเป็นยาเสพติด
Sok

1

LUA 186 ตัวอักษร

s=io.read(1)v=io.read(2)d=0 r=0 for i=1,#s do t=0 for j = 1, #s do t=t+s[i][j]end d=d+s[i][i] r=r+s[i][#s-i+1]if t ~= v then o=true end end if d~=v and r~= v then o=true end print(not o)

1

05AB1E , 24 ไบต์

ô©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สำหรับความจริง / เท็จตามลำดับ

ลองมันออนไลน์หรือตรวจสอบกรณีทดสอบบางมากขึ้น

คำอธิบาย:

ô       # 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)
โดยการใช้ไซต์ของเรา หมายความว่าคุณได้อ่านและทำความเข้าใจนโยบายคุกกี้และนโยบายความเป็นส่วนตัวของเราแล้ว
Licensed under cc by-sa 3.0 with attribution required.