2010年2月26日 星期五

2010/02/26 奇數階魔方陣

Problem Description


所謂的 n階魔方陣,就是把1到n 的這n 個 連續的正 整數填到一個 n×n的方陣中,使得每一列的和、每一行的和、以及兩個對角線的和都相等。請設計一個程式可以完成n為奇數的魔方陣。

Input

n (奇數)

Output

奇數階魔方陣
Sample Input

請輸入 n: 5
Sample Output

17 , 24 , 1 , 8 , 15
23 , 5 , 7 , 14 , 16
4 , 6 , 13 , 20 , 22
10 , 12 , 19 , 21 , 3
11 , 18 , 25 , 2 , 9

5 則留言:

  1. Dim a(22, 22) As Integer
    Private Sub Form_Load()
    Open App.Path & "/in.txt" For Input As #1
    Input #1, x
    xx = x
    Close #1
    v = 1
    p = (x - 1) / 2
    q = 0
    For i = 1 To x ^ 2 + x
    If a(q, p) = 0 Then
    a(q, p) = v
    v = v + 1
    If q - 1 < 0 Then
    q = x - 1
    Else
    q = q - 1
    End If
    If p + 1 > x - 1 Then
    p = 0
    Else
    p = p + 1
    End If
    Else
    If q + 1 > x - 1 Then
    q = 0
    Else
    q = q + 1
    End If
    If q + 1 > x - 1 Then
    q = 0
    Else
    q = q + 1
    End If
    If p - 1 < 0 Then
    p = x - 1
    Else
    p = p - 1
    End If
    End If
    Next i
    Open App.Path & "/out.txt" For Output As #2
    For i = 0 To x - 1
    For j = 0 To x - 1
    If j = x - 1 Then
    Print #2, a(i, j)
    Else
    Print #2, a(i, j) & ",";
    End If
    Next j
    Print #2,
    Next i
    Close #2
    End Sub

    回覆刪除
  2. 高仔好,
    這題你寫的似乎是對的,這樣的題目要原先就知道怎麼寫出這個魔方陣,才能用二維陣列來辛苦表現出來。
    你寫完程式,還請將輸入與輸出例子給順道貼出來啊,像是用個3和9的。

    回覆刪除
  3. Private Sub Form_Load()
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    Input #1, num
    ReDim x(num, num) As Integer
    lef = (num + 1) \ 2
    rig = 1
    kk = 1
    x(lef, rig) = kk
    kk = kk + 1
    Do Until kk = num ^ 2 + 1
    q = lef: w = rig
    lef = (lef Mod num) + 1
    rig = rig - 1
    If rig = 0 Then rig = num
    If x(lef, rig) = 0 Then
    x(lef, rig) = kk
    Else
    lef = q: rig = w
    rig = (rig Mod num) + 1
    x(lef, rig) = kk
    End If
    kk = kk + 1
    Loop
    For i = 1 To num
    For j = 1 To num
    Print #2, x(j, i);
    Next j
    Print #2,
    Next i
    Close #1
    Close #2
    End Sub


    輸入9
    輸出
    47 58 69 80 1 12 23 34 45
    57 68 79 9 11 22 33 44 46
    67 78 8 10 21 32 43 54 56
    77 7 18 20 31 42 53 55 66
    6 17 19 30 41 52 63 65 76
    16 27 29 40 51 62 64 75 5
    26 28 39 50 61 72 74 4 15
    36 38 49 60 71 73 3 14 25
    37 48 59 70 81 2 13 24 35

    老師我有個問題@@
    rig = rig - 1
    If rig = 0 Then rig = num
    可以把它變成 比較簡單的嗎 一值想不出來@@

    回覆刪除
  4. Private Sub Form_Load()
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    Input #1, num
    ReDim x(num, num) As Integer
    lef = (num + 1) \ 2
    rig = 1
    kk = 1
    x(lef, rig) = kk
    kk = kk + 1
    Do Until kk = num ^ 2 + 1
    q = lef: w = rig
    lef = (lef Mod num) + 1
    rig = rig - 1
    If rig = 0 Then rig = num
    If x(lef, rig) = 0 Then
    x(lef, rig) = kk
    Else
    lef = q: rig = w
    rig = (rig Mod num) + 1
    x(lef, rig) = kk
    End If
    kk = kk + 1
    Loop
    For i = 1 To num
    For j = 1 To num
    Print #2, x(j, i);
    Next j
    Print #2,
    Next i
    Close #1
    Close #2
    End Sub

    輸入9
    輸出
    47 58 69 80 1 12 23 34 45
    57 68 79 9 11 22 33 44 46
    67 78 8 10 21 32 43 54 56
    77 7 18 20 31 42 53 55 66
    6 17 19 30 41 52 63 65 76
    16 27 29 40 51 62 64 75 5
    26 28 39 50 61 72 74 4 15
    36 38 49 60 71 73 3 14 25
    37 48 59 70 81 2 13 24 35

    回覆刪除
  5. Y揚好,
    從結果看,你的程式是正確的。
    那兩行你是想看能不能用數學方式成為一行吧。
    例如用 mod 或是 \
    但是,似乎一般用在加1的比較容易。這題的話,
    if rig=1 then rig=num else rig=rig -1
    可以變成一行。

    rig = (rig -1 + num -1) mod num +1
    rig = (rig -2 + num ) mod num + 1

    例:num = 9
    rig=(rig-1+8) mod 9 +1
    (也就是 rig= (rig +7) mod 9 + 1)

    回覆刪除