2010年9月17日 星期五

'魔術矩陣

'魔術矩陣
' 68  81  94 107 120   1  14  27  40  53  66
' 80  93 106 119  11  13  26  39  52  65  67
' 92 105 118  10  12  25  38  51  64  77  79
'104 117   9  22  24  37  50  63  76  78  91
'116   8  21  23  36  49  62  75  88  90 103
'  7  20  33  35  48  61  74  87  89 102 115
' 19  32  34  47  60  73  86  99 101 114   6
' 31  44  46  59  72  85  98 100 113   5  18
' 43  45  58  71  84  97 110 112   4  17  30
' 55  57  70  83  96 109 111   3  16  29  42
' 56  69  82  95 108 121   2  15  28  41  54
Public Class Form1
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim str1 = ""
        Dim m = 11
        Dim a(m - 1, m - 1)
        For i = 0 To m - 1
            For j = 0 To m - 1
                a(i, j) = 0
            Next
        Next
        Dim x = m \ 2, y = 0
        Dim k = 1
        a(x, y) = k
        k = k + 1
        While k <= m * m
            Dim xb = x
            Dim yb = y
            x = (x + 1) Mod m
            y = (y - 1 + m) Mod m
            If a(x, y) = 0 Then
                a(x, y) = k
                k = k + 1
            Else
                x = xb
                y = yb
                y = (y + 1) Mod m
                a(x, y) = k
                k = k + 1
            End If
        End While
        For y = 0 To m - 1
            For x = 0 To m - 1
                'str1 = str1 & a(x, y) & vbTab
                str1 = str1 & format1(a(x, y)) & vbTab
            Next
            str1 = str1 & vbNewLine
        Next
        MsgBox(str1)
        My.Computer.FileSystem.WriteAllText(Application.StartupPath & "\Test.txt", str1, False)
        End
    End Sub
    Function format1(ByVal n)
        If n < 10 Then Return Space(2) & n
        If n < 100 Then Return Space(1) & n
        Return n
    End Function
End Class

2010年9月10日 星期五

93q6

Public Class Form1
    Private Sub Form1_Load(ByVal sender1 As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim i, j
        Dim str2 = ""
        Dim str1 = ""
        Dim a(51, 51)
        a(1, 0) = 0 : a(1, 1) = 1 : a(1, 2) = 0
        a(2, 0) = 0 : a(2, 1) = 1 : a(2, 2) = 2 : a(2, 3) = 1 : a(2, 4) = 0
        str1 = 1
        str2 = str2 & str1 & vbNewLine
        str1 = 1 & ", " & 2 & ", " & 1
        str2 = str2 & str1 & vbNewLine
        For j = 3 To 10
            str1 = ""
            a(j, 1) = 1
            str1 = str1 & 1 & ", "
            For i = 2 To j
                a(j, i) = a(j - 1, i - 1) + a(j - 1, i)
                str1 = str1 & a(j, i) & ", "
            Next
            a(j, i) = 1
            str1 = str1 & 1
            str2 = str2 & str1 & vbNewLine
        Next j
        MsgBox(str2)
    End Sub
End Class