|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
粗粗编一个,不一定是最优的- Option Explicit
- Public Enum DirectionEnum
- ToRight = 0
- ToDown = 1
- ToUp = 2
- ToLeft = 3
- End Enum
- '蛇形
- Sub SnakeMatrix(X As Long, Y As Long, Optional Direction As DirectionEnum = 0)
- Dim iX As Long
- Dim iY As Long
- Dim n As Long
- Dim DT As DirectionEnum
- ReDim mat(1 To X, 1 To Y) As Long
- DT = Direction
- iX = 1
- iY = 1
- n = 1
- mat(1, 1) = 1
- Do While n < X * Y
- n = n + 1
- If DT = 0 And (iY = 1 Or iX = X) Then
- If iX < X Then
- iX = iX + 1
- Else
- iY = iY + 1
- End If
- DT = 1
- ElseIf DT = 1 And (iX = 1 Or iY = Y) Then
- If iY < Y Then
- iY = iY + 1
- Else
- iX = iX + 1
- End If
- DT = 0
- ElseIf DT = 0 Then
- iX = iX + 1
- iY = iY - 1
- ElseIf DT = 1 Then
- iX = iX - 1
- iY = iY + 1
- End If
- mat(iX, iY) = n
- Loop
- '显示
- Dim TempTx As String
- For iY = 1 To Y
- For iX = 1 To X
- Sheet1.Cells(iY, iX) = mat(iX, iY)
- Next
- Next
- End Sub
- '旋转
- Sub RevolveMatrix(X As Long, Y As Long, Optional Direction As DirectionEnum = 0)
- Dim iX As Long
- Dim iY As Long
- Dim n As Long
- Dim DT As DirectionEnum
- ReDim mat(1 To X, 1 To Y) As Long
- DT = Direction
- iX = 1
- iY = 1
- n = 1
- mat(1, 1) = 1
- Do While n < X * Y
- n = n + 1
- Select Case DT
- Case 0
- If iX = X Then
- iY = iY + 1
- DT = ToDown
- Else
- If mat(iX + 1, iY) = 0 Then
- iX = iX + 1
- Else
- iY = iY + 1
- DT = ToDown
- End If
- End If
- Case 1
- If iY = Y Then
- iX = iX - 1
- DT = ToLeft
- Else
- If mat(iX, iY + 1) = 0 Then
- iY = iY + 1
- Else
- iX = iX - 1
- DT = ToLeft
- End If
- End If
- Case 2
- If iY = 1 Then
- iX = iX + 1
- DT = ToRight
- Else
- If mat(iX, iY - 1) = 0 Then
- iY = iY - 1
- Else
- iX = iX + 1
- DT = ToRight
- End If
- End If
- Case 3
- If iX = 1 Then
- iY = iY - 1
- DT = ToUp
- Else
- If mat(iX - 1, iY) = 0 Then
- iX = iX - 1
- Else
- iY = iY - 1
- DT = ToUp
- End If
- End If
- End Select
- Debug.Print iX & " " & iY
- mat(iX, iY) = n
- Loop
- '显示
- Dim TempTx As String
- For iY = 1 To Y
- For iX = 1 To X
- Sheet1.Cells(iY, iX) = mat(iX, iY)
- Next
- Next
- End Sub
复制代码
|
评分
-
2
查看全部评分
-
|