我做了一个你需要的上三角的,你完善一下就可以了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
完整的程序,有不足之处请指正 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
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
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