|
也湊个热闹,支持顺时、逆时,支持任意步长,支持任意方向……
- '顺时针起始方向
- Enum Clockwise_Start
- Top = 0
- Right = 1
- Bottom = 2
- Left = 3
- End Enum
- '逆时针起始方向
- Enum AntiClockwise_Start
- Top = 0
- Right = 3
- Bottom = 2
- Left = 1
- End Enum
- Const Clockwise As String = "T,R,B,L,T,R,B,L" '顺时针方向
- Const AntiClockwise As String = "T,L,B,R,T,L,B,R" '逆时针方向
- Dim lngStartNum As Long '数字初始值
- Dim lngStep As Long '步长值
- Dim lngRowID As Long, lngColID As Long '行列标号
- Dim lngCount As Long '计数
- Dim arrDirection() As String '运动方向数组
- Dim curDirection As Integer '当前方向索引
- Dim lngMax As Long '最大维度
- Dim arrResult As Variant '结果集
- Dim blIsOk As Boolean
- Dim lngStepCount As Long
- '主入口,测试
- Sub Main()
- '从9开始的11*11矩阵
- lngMax = 11 '最大维度
- 'arrDirection = SetDirection(AntiClockwise, AntiClockwise_Start.Top) '逆时针,先向上
- arrDirection = SetDirection(AntiClockwise, AntiClockwise_Start.Bottom) '逆时针,先向下
- curDirection = 0: lngStepCount = 1
- lngStartNum = 9: lngStep = 2 '初始为9,步长为2
- lngRowID = (lngMax + 1) / 2: lngColID = (lngMax + 1) / 2
- ReDim arrResult(1 To lngMax, 1 To lngMax) As Long
-
- arrResult(lngRowID, lngColID) = lngStartNum '中间填入初始值
- blIsOk = False
- Do Until blIsOk = True
- SetResult
- Loop
-
- Sheet1.Range("A1").Resize(UBound(arrResult), UBound(arrResult, 2)) = arrResult
- MsgBox "OK"
- End Sub
- '运算过程
- Function SetResult()
- Dim lngR As Long, lngC As Long
- For lngR = 1 To 2
- For lngC = 1 To lngStepCount
- lngCount = lngCount + 1
- If lngCount >= (lngMax ^ 2) Then
- blIsOk = True
- Exit Function
- End If
- lngStartNum = lngStartNum + lngStep
- Select Case arrDirection(curDirection)
- Case "T"
- lngRowID = lngRowID - 1
- Case "B"
- lngRowID = lngRowID + 1
- Case "L"
- lngColID = lngColID - 1
- Case "R"
- lngColID = lngColID + 1
- End Select
- arrResult(lngRowID, lngColID) = lngStartNum
- Next
- curDirection = (curDirection + 1) Mod 4
- Next
-
- lngStepCount = lngStepCount + 1
- End Function
- '返回运动方向
- Function SetDirection(strDirection As String, intStartDir As Integer) As Variant
- Dim strT() As String, intI As Integer
- Dim arrTemp(0 To 3) As String
- strT = Split(strDirection, ",")
- For intI = 0 To 3
- arrTemp(intI) = strT(intStartDir + intI)
- Next
- SetDirection = arrTemp
- End Function
复制代码 |
评分
-
1
查看全部评分
-
|