用vb输出如下图形
1  3  4  10  11
2  5  9  12  19
6  8  13 18  20
7  14 17 21  24
15 16 22 23  25

解决方案 »

  1.   

    我做了一个你需要的上三角的,你完善一下就可以了Dim i As Integer            '矩阵大小
    Dim Mix() As Integer        '矩阵
    Dim iSaveVal As Integer     '保存上一个位置的值
    Dim row, col As Integer     '行、列
    Dim way As String           '数字行走方向(down、rightup、right、leftdown)Private Sub Command1_Click()
    Dim iCount As Integer
    Dim nX As Integer
    i = InputBox("请输入一个值")
    ReDim Mix(1 To i, 1 To i)
        For row = 1 To i
            For col = 1 To i
                If (row = 1) Or (col = 1) Then
                    Mix(row, col) = -1
                Else
                    Mix(row, col) = 0
                End If
            Next
        Next
        For nX = 1 To i
            iCount = iCount + nX   '上三角个数(包括对角线)
        Next
        Mix(1, 1) = 1  '初始化第一个数的值
        way = "down"   '初始化方向
        row = 1
        col = 1  '初始化位置
        iSaveVal = Mix(1, 1)
        
        Do While iCount - 1
            
            Select Case way
                Case "down"
                    row = row + 1
                    If Mix(row, col) = -1 Then
                        way = "rightup"
                    End If
                    Mix(row, col) = iSaveVal + 1
                    iSaveVal = Mix(row, col)
                    Debug.Print row & ",,," & col & "..." & Mix(row, col)
                Case "rightup"
                    row = row - 1
                    col = col + 1
                    If Mix(row, col) = -1 Then
                        way = "right"
                    End If
                    Mix(row, col) = iSaveVal + 1
                    iSaveVal = Mix(row, col)
                    Debug.Print row & ",,," & col & "..." & Mix(row, col)
                Case "right"
                    col = col + 1
                    If Mix(row, col) = -1 Then
                        way = "leftdown"
                    End If
                    Mix(row, col) = iSaveVal + 1
                    iSaveVal = Mix(row, col)
                    Debug.Print row & ",,," & col & "..." & Mix(row, col)
                Case "leftdown"
                    row = row + 1
                    col = col - 1
                    If Mix(row, col) = -1 Then
                        way = "down"
                    End If
                    Mix(row, col) = iSaveVal + 1
                    iSaveVal = Mix(row, col)
                    Debug.Print row & ",,," & col & "..." & Mix(row, col)
                End Select
            iCount = iCount - 1
        Loop
    End Sub
      

  2.   

    完整的程序,有不足之处请指正
    Option ExplicitDim i As Integer            '矩阵大小
    Dim Mix() As Integer        '矩阵
    Dim iSaveVal As Integer     '保存上一个位置的值
    Dim row, col As Integer     '行、列
    Dim way As String           '数字行走方向(down、rightup、right、leftdown)Private Sub Command1_Click()
    Dim iCount As Integer
    Dim nX As Integer
    i = InputBox("请输入一个值")
    ReDim Mix(1 To i, 1 To i)
        For row = 1 To i
            For col = 1 To i
                If (row = 1) Or (col = 1) Or (row = i) Or (col = i) Then
                    Mix(row, col) = -1
                Else
                    Mix(row, col) = 0
                End If
            Next
        Next
        For nX = 1 To i
            iCount = iCount + nX   '上三角个数(包括对角线)
        Next
        Mix(1, 1) = 1  '初始化第一个数的值
        way = "down"   '初始化方向
        row = 1
        col = 1  '初始化位置
        iSaveVal = Mix(1, 1)
        
        Do While iCount - 1
            
            Select Case way
                Case "down"
                    row = row + 1
                    If Mix(row, col) = -1 Then
                        way = "rightup"
                    End If
                    Mix(row, col) = iSaveVal + 1
                    iSaveVal = Mix(row, col)
                    Debug.Print row & ",,," & col & "..." & Mix(row, col)
                Case "rightup"
                    row = row - 1
                    col = col + 1
                    If Mix(row, col) = -1 Then
                        way = "right"
                    End If
                    Mix(row, col) = iSaveVal + 1
                    iSaveVal = Mix(row, col)
                    Debug.Print row & ",,," & col & "..." & Mix(row, col)
                Case "right"
                    col = col + 1
                    If Mix(row, col) = -1 Then
                        way = "leftdown"
                    End If
                    Mix(row, col) = iSaveVal + 1
                    iSaveVal = Mix(row, col)
                    Debug.Print row & ",,," & col & "..." & Mix(row, col)
                Case "leftdown"
                    row = row + 1
                    col = col - 1
                    If Mix(row, col) = -1 Then
                        way = "down"
                    End If
                    Mix(row, col) = iSaveVal + 1
                    iSaveVal = Mix(row, col)
                    Debug.Print row & ",,," & col & "..." & Mix(row, col)
                End Select
            iCount = iCount - 1
        Loop
        
        iCount = 0
        For nX = 1 To i
            iCount = iCount + nX
        Next
        iCount = i * i - iCount   '下三角个数
        row = i
        col = 1
        way = "right"
        Do While iCount
            Select Case way
            Case "right"
                col = col + 1
                If Mix(row, col) = -1 Then
                    way = "rightup"
                End If
                Mix(row, col) = iSaveVal + 1
                iSaveVal = Mix(row, col)
                Debug.Print row & ",,," & col & "..." & Mix(row, col)
            Case "rightup"
                row = row - 1
                col = col + 1
                If Mix(row, col) = -1 Then
                    way = "down"
                End If
                Mix(row, col) = iSaveVal + 1
                iSaveVal = Mix(row, col)
                Debug.Print row & ",,," & col & "..." & Mix(row, col)
            Case "down"
                row = row + 1
                If Mix(row, col) = -1 Then
                    way = "leftdown"
                End If
                Mix(row, col) = iSaveVal + 1
                iSaveVal = Mix(row, col)
                Debug.Print row & ",,," & col & "..." & Mix(row, col)
            Case "leftdown"
                row = row + 1
                col = col - 1
                If Mix(row, col) = -1 Then
                    way = "right"
                End If
                Mix(row, col) = iSaveVal + 1
                iSaveVal = Mix(row, col)
                Debug.Print row & ",,," & col & "..." & Mix(row, col)
            End Select
            iCount = iCount - 1
        Loop
    End Sub