2012年9月6日 星期四

4K魔方陣


既然有了奇數的魔方陣

那麼來嘗試下偶數的 ^^







輸入說明:

輸入一數 n (0<n<10000,且n mod 4 =0)

輸出說明:
請照題目規則,輸出n行n列的矩陣,且直行橫列對角線總和都要相等。

個位數前請補0對齊




輸入範例:

4



輸出範例:

01 15 14 04

12 06 07 09

08 10 11 05

13 03 02 16

1 則留言:

  1. 類似土法煉鋼的,大概知道規律,但還寫不出規律來。
    只有4跟8而已。
    Dim x As Long
    Dim a()
    Private Sub Form_Load()
    Me.Hide
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    b = 1
    c = 1
    Input #1, n
    x = n * n
    ReDim a((n / 2), n, n)
    e = 16
    For i = 1 To (n / 2)
    Call re(b, i, e)
    b = b + 16
    e = e + 16
    Next
    If n = 8 Then
    For i = 1 To n
    c = a(1, i, 1)
    a(1, i, 1) = a(4, i, 1)
    a(4, i, 1) = c
    c = a(1, 3, i)
    a(1, i, 3) = a(4, i, 3)
    a(4, i, 3) = c
    c = a(2, i, 1)
    a(2, i, 1) = a(3, i, 1)
    a(3, i, 1) = c
    c = a(2, i, 3)
    a(2, i, 3) = a(3, i, 3)
    a(3, i, 3) = c
    Next
    For i = 1 To 4
    f = 1
    For j = 1 To 4
    Print #2, a(f, i, j);
    Next
    f = 3
    For j = 1 To 4
    Print #2, a(f, i, j);
    Next
    Print #2,
    Next
    For i = 1 To 4
    f = 4
    For j = 1 To 4
    Print #2, a(f, i, j);
    Next
    f = 2
    For j = 1 To 4
    Print #2, a(f, i, j);
    Next
    Print #2,
    Next
    End If
    If n = 4 Then
    For i = 1 To 4
    For j = 1 To 4
    Print #2, a(1, i, j);
    Next
    Print #2,
    Next
    End If
    Close
    Close
    End
    End Sub
    Sub re(b, c, e)
    For i = b To e
    Select Case i Mod 16
    Case 0
    a(c, 4, 1) = i
    Case 1
    a(c, 1, 1) = i
    Case 2
    a(c, 2, 1) = i
    Case 3
    a(c, 3, 2) = i
    Case 4
    a(c, 4, 2) = i
    Case 5
    a(c, 4, 3) = i
    Case 6
    a(c, 3, 4) = i
    Case 7
    a(c, 2, 3) = i
    Case 8
    a(c, 1, 4) = i
    Case 9
    a(c, 4, 4) = i
    Case 10
    a(c, 3, 3) = i
    Case 11
    a(c, 2, 4) = i
    Case 12
    a(c, 1, 3) = i
    Case 13
    a(c, 1, 2) = i
    Case 14
    a(c, 2, 2) = i
    Case 15
    a(c, 3, 1) = i
    End Select
    Next
    End Sub

    回覆刪除