解奇數方型魔方陣。

Module Module1
    Sub Main()
        Dim n, x, y, i As Integer
        Dim s(10, 10) As Integer
        n = Console.ReadLine()

        For y = 0 To n - 1
            For x = 0 To n - 1
                s(y, x) = 0
            Next
        Next

        x = (n - 1) / 2
        y = 0
        s(y, x) = 1

        For i = 2 To n * n
            y = IIf(y = 0, n - 1, y - 1)
            x = (x + 1) Mod n
            If (s(x, y)) <> 0 Then
                y = (y + 2) Mod n
                x = IIf(x = 0, n - 1, x - 1)
            End If
            s(y, x) = i
        Next

        For y = 0 To n - 1
            For x = 0 To n - 1
                Console.Write(s(y, x) & "")
            Next
            Console.WriteLine()
        Next

        Console.ReadKey()
    End Sub
End Module
創作者介紹

漫遊於歷史與現代之間

okplaymayday 發表在 痞客邦 PIXNET 留言(0) 人氣()